Solved

Start timer at next fixed interval

Posted on 2014-12-27
6
139 Views
Last Modified: 2014-12-27
I am using a timer but I don't want it to start until the next exact 10s slot

By this I mean

12/12/2014 16:02:14.000 should start at 12/12/2014 16:02:20.000

Thanks for the help
0
Comment
Question by:SweetingA
  • 3
  • 2
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40519587
Then you have two timers.  The first just to check for the start time of the second timer and set that going when it is required.
0
 
LVL 3

Expert Comment

by:Ali HND
ID: 40519588
Set timer interval to 100
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Dim s As Integer = DateTime.Now.Second
        If s Mod 10 = 0 Then
            'Something that you need to do
        End If
    End Sub

Open in new window

0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40519594
Oh and one other thing.
next exact 10s slot
Timer events are fired when the system is not busy.  You can never guarantee it will be at .000 seconds no matter what you set as the timer interval.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 3

Expert Comment

by:Ali HND
ID: 40519602
@AndyAinscow
I think, by decreasing interval of timer to 100, 10 or 1 that's possible to do.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40519608
>>I think, by decreasing interval of timer to 100, 10 or 1 that's possible to do.

Sorry but that is not correct.  Various services run, numbers of programs run.  Each are given a time slice.  You can never be certain that time slice will be cover eg. 16:02:20.000
The time slice might start at 16:02:20.001 or it might finish at 16:02:19.999 or even further away OR, by chance, it might be at 16:02:20.000.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40519731
Instead of two timers, I like to calculate the milliseconds to the next target time like this:
Public Class Form1

	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
		Timer1.Interval = IntervalToNextTarget()
		Timer1.Start()
	End Sub

	Private Function IntervalToNextTarget() As Integer
		Dim now As DateTime = DateTime.Now
		Return CInt(now.AddSeconds(10 - (now.Second Mod 10)).Subtract(now).TotalMilliseconds)
	End Function

	Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
		' ... do something ...
		Debug.Print(DateTime.Now)

		Timer1.Interval = IntervalToNextTarget()
	End Sub

End Class

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now