22.6 Creating an Application Timer

 < Day Day Up > 

You want your application to perform some task on a background thread at regular intervals.


Technique

To have some background task performed at regular intervals, you must first define the method that will be called at the set intervals (the timer callback method). This method must have the signature void MethodName(object state) . As usual, this method can be a static or instance member of any class. Then, you should do the following:

  1. Wrap this method in a System.Threading.TimerCallback delegate.

  2. Instantiate a System.Threading.Timer object, passing it this delegate as well as information specifying the interval and an object reference containing any additional data you want to pass to the timer callback method.

First, assume that this method needs to be called at regular intervals:

 
 class TimerHandler {         public void UpdateApp(object state)         {                 Console.WriteLine("Timer got called again!");         } } 

Now you arrange to have this method called on a timer:

 
 TimerHandler timerHandlerObject = new TimerHandler(); TimerCallback timerCallback = new TimerCallback(timerHandlerObject.UpdateApp); Timer timer = new Timer(timerCallback, null, 2000, 500); 

This code shows that you must pass several parameters to the timer constructor. The first parameter is the delegate that identifies the timer callback method. The second parameter is passed to the timer callback method as the state parameter every time the timer has this method called. A typical use might be to provide information about the source of the timer, although you are free to simply use null as we did in this code. The third parameter is the time that the timer will wait until it first calls the timer callback methodknown as the due timeand the final parameter is the interval between callbacks thereafter (the period). Both parameters are measured in milliseconds ; so with the preceding code, the timer will start callbacks in two seconds and then repeat every half-second.

Note that there are several overloads of the Timer constructor, but they differ only in the data type used to specify the due time and period. You can pass the times in milliseconds as long or as uint , or, more interestingly, you can pass them as TimeSpan objects. The preceding code is completely equivalent to this:

 
 TimerHandler timerHandlerClass = new TimerHandler(); TimerCallback timerCallback = new TimerCallback(timerHandlerClass.UpdateApp); TimeSpan dueTime = new TimeSpan(0, 0, 0, 0, 2000); TimeSpan period = new TimeSpan(0, 0, 0, 0, 500); Timer timer = new Timer(timerCallback, "Hello", dueTime, period); 

If you subsequently want to disable the timer, the easiest way is to use its Change() method to change the due time and timer interval to infinity:

 
 timer.Change(Timeout.Infinite, Timeout.Infinite); 

Timeout.Infinite simply returns the int value -1 which the timer interprets as infinite.

Comments

As you might expect from a chapter on multithreading, the System.Threading.Timer class uses multiple threads: In fact, the callback method is always executed on a thread-pool thread. Because the thread pool is used, you should be aware that there is no guarantee that the same thread will be used in successive timer callbacks.

Thanks to its use of the thread pool, this timer is, provided there are thread-pool threads free, highly accurate. It is the recommended timer to use if you need an accurate timer interval. However, the CLR does make two other timers available: System.Windows.Forms.Timer and System.Timers.Timer . The Windows Forms timer is event driven: It works by placing events on the message loop at intervals and therefore does not rely on multiple threads. It is simple to use in a Windows Forms application, but it is not very accurate because if the main thread is engaged in some other task when the timer event is due, the event simply sits on the message loop unprocessed until the main thread becomes free again. The System.Timers.Timer class is similarly event-based but is intended for Web server applications. We don't consider either of these timers in this book.

 < Day Day Up > 


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net