About the Timer class in C #
Timer There are three classes.
1. defined in system. Windows. Forms
2. defined in the system. Threading. Timer class
3. defined in the system. Timers. Timer class
System. Windows. Forms. Timer is used in winform. It is implemented through the windows message mechanism. Similar to the timer control in VB or Delphi, it is implemented using the API settimer internally. Its main disadvantage is that the timing is not accurate, and there must be a message loop, console application ( Console Application.
System. Timers. timer and system. Threading. timer are very similar. They are implemented through the. NET thread pool. They are lightweight and time-precise, and have no special requirements on applications and messages. System. Timers. timer can also be used in winform to completely replace the above Timer control. Their disadvantage is that they do not support direct drag and drop and require manual encoding.
Example:
Use the system. Timers. Timer class
System. Timers. Timer T = new system. Timers. Timer (10000); // instantiate the Timer class and set the interval Time It is 10000 milliseconds;
T. elapsed + = new system. Timers. elapsedeventhandler (theout); // Time of Arrival Run Event;
T. autoreset = true; // set whether to execute once (false) or always execute (true );
T. Enabled = true; // whether to execute the system. Timers. Timer. elapsed event;
Public void theout (Object source, system. Timers. elapsedeventargs E)
{
MessageBox. Show ("OK! ");
}
Private void button#click (Object sender, system. eventargs e )...{
System. Timers. Timer T = new system. Timers. Timer (1*60*60*1000 );
T. elapsed + = new system. Timers. elapsedeventhandler (timer_timesup );
T. Enabled = true;
}
Private void timer_timesup (Object sender, system. Timers. elapsedeventargs e )...{
MessageBox. Show ("hello ");
}
// If a button is required to be triggered:
Private void form1_load (Object sender, system. eventargs e )...{
System. Timers. Timer T = new system. Timers. Timer (1*60*60*1000 );
T. elapsed + = new system. Timers. elapsedeventhandler (timer_timesup );
T. Enabled = true;
}
Private void button#click (Object sender, system. eventargs e )...{
MessageBox. Show ("hello ");
}
Private void timer_timesup (Object sender, system. Timers. elapsedeventargs e )...{
This. button1.w.mclick ();
}
What is the difference between the timer in the application and the timer in the component?
Server timer, Windows timer, and thread Timer In Visual Studio. net and. NET framework has three timer controls: Server-based timer, located on the "components" tab of the "toolbox"; Windows-based standard timer, located on the Windows Forms tab of the toolbox, and thread timers that can only be used during programming. Windows-based timers exist in Visual Basic Version 1.0 and remain unchanged. This timer has been optimized for use in Windows Forms applications. A server-based timer is an updated version of a traditional timer optimized to run in a server environment. A thread timer is a simple and lightweight timer that uses a callback method instead of an event and is provided by the thread pool thread.
There are two types of threads in the Win32 architecture: Ui thread and auxiliary thread. The UI thread is idle most of the time and waits for messages in the message loop to arrive. Once a message is received, it is processed and waits for the next message to arrive. In addition, the auxiliary thread is used to execute background processing without using message loops. Windows timer and server-based timer use the interval attribute when running. The interval of the thread timer is set in the timer constructor. Timers are designed for different purposes, and their thread processing explicitly points out this:
The Windows timer is designed for a Single-threaded environment, where the UI thread is used for processing. The Windows timer precision is limited to 55 milliseconds. These traditional timers require the user code to have an available UI message pump, and always operate in the same thread, or send calls to another thread. For COM components, this will reduce the performance. Server-based timers are designed to be used together with auxiliary threads in a multi-threaded environment. Because they use different architectures, server-based timers may be much more accurate than Windows timers. The server timer can be moved between threads to process the events. Thread timers are very useful in scenarios where messages are not sent by threads. For example, a Windows-based timer depends on the support of the operating system timer. If a message is not sent on the thread, the timer-related event will not occur. In this case, the thread timer is very useful. The Windows timer is located in the system. Windows. Forms namespace, the server timer is located in the system. Timers namespace, And the thread timer is located in the system. Threading namespace. |
Timer Components
TimerA component is a server-based timer that allows 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 createTimerTo 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-basedTimerIt is designed for auxiliary threads in a multi-threaded environment. The server timer can be moved between threads to processElapsedIn this way, you can more accurately trigger events on time than Windows timers. For more information about server-based timers, see "server-based timers ". Value Based on the interval attribute,TimerComponent triggeringElapsedEvent. 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. AvailableTimerBatch Processing is started every 30 minutes.
When autoreset is setFalse,TimerOnly in the firstIntervalTriggered once laterElapsedEvent. To maintainIntervalTime IntervalElapsedEvent, pleaseAutoresetSetTrue. |
ElapsedThe event is triggered on the threadpool thread. IfElapsedEvent processing time ratioIntervalLong, in anotherThreadpoolThe thread will trigger this event again. Therefore, the event handler should be reentrant. The following example createsTimerIt displays "Hello World!" on the console every five seconds !". Using system; Using system. Timers; Public class timer1 { Public static void main () { // Normally, the timer is declared at the class level, so // That it doesn' t go out of scope when the method ends. // In this example, the timer is needed only while Main // Is executing. However, keepalive must be used at // End of main, to prevent the JIT compiler from allowing // Aggressive garbage collection to occur before Main // Ends. System. Timers. Timer atimer = new system. Timers. Timer (); // Hook up the elapsed event for the timer. Atimer. elapsed + = new elapsedeventhandler (ontimedevent ); // Set the interval to 2 seconds (2000 milliseconds ). Atimers. interval = 2000; Atimer. Enabled = true; Console. writeline ("press the Enter key to exit the program ."); Console. Readline (); // Keep the timer alive until the end of main. GC. keepalive (atimer ); } // Specify what you want to happen when the elapsed event is // Raised. Private Static void ontimedevent (Object source, elapsedeventargs E) { Console. writeline ("Hello world! "); } } |