Solved

Start timer at next fixed interval

Posted on 2014-12-27
6
143 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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