Solved

Start timer at next fixed interval

Posted on 2014-12-27
6
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 86

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to remove duplicate code from my project 5 49
modifying VBScript to VBA 17 74
Adding where clause to entity statement c# 1 48
Convert VB web project to C# 3 40
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 …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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