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
Share
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
Timerclass 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:
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.TimeChangedto know when that happens.