Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...

Wednesday, December 14


Timers allow you to specify a recurring interval at which an event can be raised in your application. .NET offers three different timer mechanisms :
  • System.Windows.Forms.Timer
  • System.Timers.Timer
  • System.Threading.Timer
Last week we went live with another release of our application in production. A major change for our software was the implementation of role-based security. Because of this release I will certainly remember the differences between the first two timer-objects. In a Windows Service we used System.Timers.Timer objects and a lot of security-exceptions [Request for Principal failed] were raised when the role-based security was set to our classes.
  • System.Windows.Forms.Timer
    This timer is optimized for use in Windows Forms applications. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code has a UI message pump available and always operates from the same thread. A Tick event is raised at intervals based on the Interval property setting.
  • System.Timers.Timer
    The server-based Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy (metronome-quality) than Windows timers. The System.Timers.Timer class will call the timer event handler on a worker thread obtained from the common language runtime (CLR) thread pool.
When using the System.Timers.Timer, be sure to set the appropriate Principal to the thread that will execute the Elapsed event because this worker thread won't inherit the Principal from the main thread. The Tick event of the System.Windows.Forms.Timer will always run on the main UI thread, but the Tick event won't be as accurate ...


Post a Comment

Links to this post:

Create a Link

<< Home