Solved

Start timer at next fixed interval

Posted on 2014-12-27
6
152 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

724 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