Introduction to Server-Based Timers
Server-based timers allow you to specify a recurring interval at which an event will be raised in your application. You can then handle this event to provide regular processing. For example, suppose you have a critical server that must be kept running 24 hours a day, seven days a week. You could create a service that uses a timer to periodically check the critical server to ensure that the system is up and running. If the system is not responding, the service can attempt to restart the server or notify an administrator.
The interval for server timers is specified in milliseconds.
There are three timer controls in Visual Studio and the .NET Framework:
A server-based timer, which you can add to the Toolbox
A Windows-based timer, which is always in the Toolbox
A thread timer, which is available programmatically
The Windows-based timer is optimized for use in Windows Forms applications. The server-based timer is an update of the traditional timer that has been optimized to run in a server environment. The thread timer is a simple, lightweight timer that uses callback methods instead of events and is served by thread-pool threads.
There are two types of threads in Win32 architecture: UI threads, and worker threads. UI threads sit idle most of the time and wait for messages to arrive in their message loops. Once they receive a message, they handle it and then wait for the next message to arrive. Alternatively, worker threads are used to perform background processing and do not use message loops. Both the Windows timer and the server-based timer run using an Interval property. The interval of the thread timer is set in the Timer constructor. The timers are designed for different purposes, as evidenced by their handling of threads:
The Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. The accuracy of Windows timers is limited to 55 milliseconds. These traditional timers require that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread. For a COM component, this would be detrimental to performance.
The server-based timer is designed for use with worker threads in a multi-threaded environment. Because they use a different architecture, server-based timers have the potential to be much more accurate than Windows timers. Server timers can move among threads to handle the raised events.
The thread timer is useful in scenarios where messages are not pumped on the thread. For example, the Windows-based timer relies on operating-system timer support, and if you are not pumping messages on the thread, the event associated with your timer will not occur. The thread timer is more useful in this case.