In C #, there are three timer classes:
1. defined in system. Windows. Forms
Implement the timer that triggers the event at the User-Defined time interval. This timer is most suitable for Windows Forms applicationsProgramAnd must be used in the window.
Namespace: system. Windows. Forms
Assembly: system. Windows. Forms (in system. Windows. Forms. dll)
Note:
Timer is used to trigger events at user-defined event intervals. The Windows timer is designed for a Single-threaded environment, where the UI thread is used for processing. It requires the userCodeThere is an available UI message pump that is always operated in the same thread, or calls are blocked to another thread.
When using this timer, use the tick event of the control to perform the round robin operation, or display the startup screen within the specified time. When the enabled attribute is set to true and the interval attribute is greater than 0, a tick event is triggered. The interval is set based on the interval attribute.
This class provides methods to set the interval and start and stop the timer.
Note: The Windows form timer component is a single-threaded component with a precision of 55 ms. If you need a more precise multi-thread timer, use the Timer class in the system. Timers namespace.
Thread security:
All public static (shared in Visual Basic) Members of this type are thread-safe, but it is not guaranteed that all instance members are thread-safe.
Platform:
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for smartphone, Windows Server 2003, Windows XP Media Center edition, Windows XP Professional x64 Edition windows XP SP2, Windows XP Starter Edition
. NET Framework does not support all versions of each platform. For a list of supported versions, see system requirements.
Version:
. NET Framework
Supported by the following versions: 2.0, 1.1, and 1.0
. NET Compact framework
Supported by versions 2.0 and 1.0
2. defined in the system. Timers. Timer class
Generate periodic events in the application.
Namespace: system. Timers
Assembly: system (in system. dll)
Note:
Note: The hostprotectionattribute attribute applied to this class has the following resources property value: synchronization | externalthreading. Hostprotectionattribute does not affect desktop applications (these applications are usually started by double-clicking the icon, typing a command, or entering a URL in a browser ). For more information, see hostprotectionattribute or SQL Server programming and host protection attributes.
The timer component is a server-based timer that enables you to specify the periodic interval at which elapsed events are triggered in an application. This event can then be manipulated to provide regular processing. For example, assume that you have a critical server that must be running 24 hours a day, 7 days a week. You can create a service that uses timer to regularly check the server and ensure that the system is enabled and running. If the system does not respond, the service can restart the server or notify the administrator.
Server-based timer is designed for auxiliary threads in multi-threaded environments. The server timer can be moved between threads to process the triggered elapsed event, so that the event can be triggered more accurately than the Windows timer. For more information about server-based timers, see "server-based timers ".
Based on the value of the interval attribute, the timer component triggers the elapsed event. This event can be processed to perform the required processing. For example, assume that you have an online sales application that continuously sends sales orders to the database. The service that compiles the delivery instruction processes the order in batches, instead of processing each order separately. You can use timer to start a batch every 30 minutes.
Note: When autoreset is set to false, Timer only triggers an elapsed event after the first interval. To keep the elapsed events triggered at interval intervals, set autoreset to true.
The elapsed event is triggered on the threadpool thread. If the processing time of the elapsed event is longer than interval, the event will be raised again in another threadpool thread. Therefore, the event handler should be reentrant.
Note: When a thread calls the stop method or sets the enabled attribute to false, it can run the event processing method on another thread. This may cause the elapsed event to be triggered after the timer is stopped. The sample code of the Stop method demonstrates a way to avoid this contention condition.
If timer is used with a user interface element (such as a form or control), assign a form or control containing timer to the synchronizingobject attribute to mail the event to the user interface thread.
Timer is invisible at runtime.
For a list of initial attribute values of a timer instance, see timer constructor.
Thread security:
Any public static member of this type is safe for multi-threaded operations. However, it cannot guarantee that any instance Member is thread-safe.
Platform:
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
. NET Framework does not support all versions of each platform. For a list of supported versions, see system requirements.
Version:
. NET Framework
Supported by the following versions: 2.0, 1.1, and 1.0
3. defined in the system. Threading. Timer class
Provides a mechanism to execute methods at a specified interval. This class cannot be inherited.
Namespace: system. threading
Assembly: mscorlib (in mscorlib. dll)
Note:
Note: The hostprotectionattribute attribute applied to this class has the following resources property value: synchronization | externalthreading. Hostprotectionattribute does not affect desktop applications (desktop applications are usually started by double-clicking the icon, typing a command, or entering a URL in a browser ). For more information, see hostprotectionattribute or SQL Server programming and host protection attributes.
Use the timercallback delegate to specify the method to be executed by the timer. The timer delegate is specified when the timer is constructed and cannot be changed. This method is not executed on the thread that creates the timer, but on the threadpool thread provided by the system.
When creating a timer, you can specify the amount of time (End Time) waiting before the first method is executed and the amount of time (cycle) waiting in the subsequent execution period ). You can use the change method to change these values or disable timers.
Note: As long as you are using timer, you must retain the reference to it. If there is no reference to timer for any managed object, the timer will be reclaimed. Timer is recycled even if it is still active. (An error occurs here, but the timer does not run when it is active)
When you no longer need a timer, use the dispose method to release the resources held by the timer. If you want to receive a signal when the timer is released, use the dispose (waithandle) method that accepts waithandle to overload it. After the timer is released, waithandle is terminated.
The callback method executed by the timer should be reentrant because it is called on the threadpool thread. In the following two cases, the callback can be executed simultaneously on two thread pools: first, the timer interval is less than the time required to execute the callback; second, all thread pool threads are in use, and the callback is queued multiple times.
Note: system. Threading. Timer is a timer that uses the callback method and is provided by the thread pool thread. It is simple and does not require high resources. You can also consider system. Windows. Forms. Timer used with Windows Forms and system. Timers. timer based on the server timer function. These timers use events and have additional functions.
Thread security:
This type is safe for multi-threaded operations.
Version:
. NET Framework
Supported by the following versions: 2.0, 1.1, and 1.0
. NET Compact framework
Supported by versions 2.0 and 1.0
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/houfeng11/archive/2010/07/27/5768815.aspx