Solved

Start timer at next fixed interval

Posted on 2014-12-27
6
147 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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