I have a code written in VB.Net that will run on multiple systems. One of the function of that code is to check for files in some folder each minute.If we run code on different system at different times, it is possible that timer will execute the function to check the files at different times on each system. Is there any way ,to make the timer running in each system in sync with each other so that it doent matter when the timer start on each system, they all will check for the file at the same time
-
Show some respect and fix the errors in your question. – Daniel Hilgarth Jan 20 '12 at 15:17
-
http://www.dotnetperls.com/timer-vbnet is a good example – parapura rajkumar Jan 20 '12 at 15:17
-
To clarify: "How do I call a function after a minute elapses according to the system clock?" – vcsjones Jan 20 '12 at 15:18
-
4I don't *think* this is an **exact** duplicate as using a timer to call something every minute. He wants to call a function every minute according to the system clock. So when the system time changes to 10:00 to 10:01 - something is called. Thats different than calling something every time a minute elapses since the last call. – vcsjones Jan 20 '12 at 15:20
-
http://stackoverflow.com/questions/1434005/getting-a-system-clock-change-tick-in-c-sharp is close. – stuartd Jan 20 '12 at 15:43
1 Answers
If I understand your question correctly, you want to call a function every minute but have it synchronized with the system clock. This isn't entirely straight forward. I'll tell you my approach and we'll go from there.
The Timer
class is a good way of executing a given function at an X interval. The problem with this is you want to keep it in sync with when a minute elapses according to the system clock. So we need to make sure we start the timer only when a minute elapses by the clock. Here are a few approaches.
Let the timer fire every second; but before we let it do anything we check to see if a minute has passed according to the clock. If so; then we let it proceed. That would look something like this:
Module TimerExample Private _previousTime As DateTime = DateTime.Now Sub Main() Dim timer As New System.Timers.Timer(1000) AddHandler timer.Elapsed, AddressOf TimeElapsed timer.Start() 'Run a loop so the console application doesn't quit Application.Run() End Sub Sub TimeElapsed(sender As Object, args As ElapsedEventArgs) Dim now As DateTime = DateTime.Now If now.Minute > _previousTime.Minute Then 'A minute elapsed. Do your code here Console.WriteLine("A minute elapsed!") End If _previousTime = now End Sub End Module
This might be an acceptable solution for you. The only "downside" is that the timer is firing every second; but it doesn't do anything until a minute has elapsed.
Another alternative might be to use two timers. Use the first timer in a similar fasion to the first example; but use it to start another timer that only elapses every minute. Once you start the second timer, let the first one stop. The only positive to this approach is a timer is only firing every minute. The negatives are the timer will probably start drifting from the system clock over a long period of time. The first example is a better approach; but this is an option if letting a timer fire every second is too expensive (though I really doubt it would be).
Another thing you may want to handle; maybe not; is when a user changes the system time. You can handle time SystemEvents.TimeChanged
to know when that happens.

- 138,677
- 31
- 291
- 286
-
Thanks for your detailed answer...is there any considerable effect on performance if we fire timer every second? – sachin Jan 31 '12 at 10:05
-
@sachin If all it is doing every second is only checking if it *should* do something, but only doing that something every minute; then the performance impact will be miniscule. – vcsjones Jan 31 '12 at 14:54