There are 3 timer classes in C#
1. Defined in
2. Defined in the class
3. Defined in the class
It is applied in WinForm. It is implemented through the Windows message mechanism, similar to the Timer control in VB or Delphi, and is implemented internally using the API SetTimer. Its main disadvantage is that the timing is inaccurate and that there must be a message loop, which the Console Application cannot be used.
Very similar, they are implemented through .NET Thread Pool, lightweight, precise timing, and have no special requirements for applications and messages. It can also be applied to WinForm, completely replacing the Timer control above. The disadvantage is that they do not support direct drag and drop and require manual encoding.
Example 1:
Use class
t = new (10000);//Instantiate the Timer class and set the interval to 10000 milliseconds;
+= new (theout);//Execute event when the time arrives;
= true;//Set whether to execute once (false) or always (true);
= true;//Whether the event is executed;
public void theout(object source, e)
{
("OK!");
}
Example 2: TimerCallback delegation of class
It is a timer that uses callback methods and is served by thread pool threads. It is simple and has low resource requirements.
As long as Timer is used, references to it must be retained. For any managed object, the timer will be garbage collected without a reference to the Timer. Even if Timer is still active, it will be recycled. When a timer is no longer needed, use the Dispose method to release the resource held by the timer.
Use TimerCallback delegate to specify the method you want Timer to execute. The timer delegate is specified when constructing the timer and cannot be changed. This method is not executed in the thread that creates the timer, but in the thread pool thread provided by the system.
When creating a timer, you can specify the amount of time to wait before the first execution of the method (deadline) and the amount of time to wait during the execution period thereafter (time period). You can use the Change method to change these values or disable the timer.
Demo application:
Application scenario: After the Windows Form program automatically performs a certain task, it hopes that its Windows Form can be automatically closed.
Code design:
(1) First declare the Timer variable: private timerClose;
(2) Add the following Timer instantiation code after the above automatic execution code:
// Create a timer thread and start it
timerClose = new (new TimerCallback(timerCall), this, 5000, 0);
Timer constructor parameter description:
Callback: A TimerCallback delegate that indicates the method to be executed.
State: An object that contains the information to be used by the callback method, or is an empty reference (Nothing in Visual Basic).
dueTime: The amount of time delayed before calling callback (in milliseconds). Specified to prevent the timer from starting timing. Specify zero (0) to start the timer immediately.
Period: The interval in milliseconds for calling callback. Specifies that periodic termination can be disabled.
(3) Define the method to be executed by TimerCallback delegate:
private void timerCall(object obj)
{
();
();
}
Of course, there should be many other ways to use the TimerCallback delegation mechanism of the above class. In addition, here is just a simple application that demoed the TimerCallback delegation.