Timers in .NET: System.Windows.Forms.Timer

The original goal of the application was to make a timed pause. Sort of like a function, which you would always call and have the app pause for certain reasons (mostly communicating with a peripheral device that operates at lower crystal frequencies than your PC CPU and thus is not quite capable of keeping up to that 2 GHz pace). If you’re writing a GUI application, .NET Framework has this System.Windows.Forms.Timer that seems to be ideally suited for the job, since it will be used inside a Windows Forms app, and I do need the event to be raised every once in a while (1 second, to be exact), so the default code for the Timer object, generated by VS.NET, looks as follows:

this.timer1.Enabled = true;
this.timer1.Interval = 1000; //milliseconds, so 1 second interval
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

Inside the timer1_Tick event I locate a variable SecondsPassedSinceLaunch that will conveniently trace – you got it – seconds since the application was launched. This is not quantum physics, so the variable is initialized to 0 on the form startup and then incremented each time inside the Timer’s tick event. My Windows app has a label that displays the current value of the SecondsPassedSinceLaunch, so everything is fine and dandy, as I am watching the form update the variable every second.

Ok, so time for Payse function now, and since my timer was initialized to fire an event every second, the Pause function will accept an int argument and wait for that many seconds.

private void Pause (int seconds)
{
   int CurrentValue = SecondsPassedSinceLaunch;
   while (SecondsPassedSinceLaunch < CurrentValue + seconds);   //do nothing
}

Seems fairly normal. Do nothing, but just sit and wait until the value of the SecondsPassedSinceLaunch surpasses the previous value that you so conveniently recorded plus the time asked for the pause. So if someone asked for a 5 second pause, the app would wait for 5 seconds, while the Tick event would update the actual timer value, and when the 6th seconds hits, we’d be out of the while loop.

Doesn’t do it. The app hangs up and really nothing happens. I suppose when the documentation says “It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread”, that’s what meant – do not initiate while loops designed to time specific pauses when using System.Windows.Forms.Timer.

According to this article, I should’ve stuck Systems.Timers.Timer:

Server timers also work by raising events, but they are more flexible than Windows Forms timers. The documentation implies that server timers are more accurate than Windows Forms timers, but doesn’t say specifically how much more accurate. In addition, server-based timers can be used in multithreaded environments. The server timer is implemented in the System.Timers.Timer class.

Posted Saturday, June 5th, 2004 under Programming.

Leave a Reply