I have reviewed the knowledge about the Thread class and the usage of the timer.
The hierarchy of timer classes is system. Object-System.MarshallByRefObject, and its constructor has the following overload methods:
The corresponding constructor is described as follows:
The timer class is a timer that regularly executes user-specified functions. When the timer starts, the system automatically creates a new thread to execute the specified function, we can initialize a timer object in the following way:
Timer timer = new timer (timerdelfgate, OBJ, 0,1000 );
The first parameter is the timercallback delegate, indicating the method to be executed;
The second parameter is an object that contains the information used by the callback method. It can be a null reference;
The third parameter is the Execution Delay Time in milliseconds. If the parameter is set to "0", the execution starts immediately.
The fourth parameter refers to the timer time interval. Every so long period of time, the method represented by timercallback will be called once, in milliseconds.
The timer class holds the following methods:
The corresponding descriptions are as follows:
The following is an example of the Timer class.Code:
View code
Using System;
Using System. Threading;
Class Timerexample
{
Static Void Main ()
{
// Create an event to signal the timeout count threshold in
// Timer callback.
Autoresetevent autoevent = New Autoresetevent ( False );
Statuschecker = New Statuschecker ( 10 );
// Create an inferred delegate that invokes methods for the timer.
Timercallback TCB = Statuschecker. checkstatus;
// Create a timer that signals the delegate to invoke
// Checkstatus after one second, and every 1/4 second
// Thereafter.
Console. writeline ( " {0} creating timer. \ n " ,
Datetime. Now. tostring ( " H: mm: Ss. fff " ));
Timer statetimer = New Timer (TCB, autoevent, 1000 , 250 );
// When autoevent signals, change the period to every
// 1/2 second.
Autoevent. waitone ( 5000 , False );
Statetimer. Change ( 0 , 500 );
Console. writeline ( " \ Nchanging period. \ n " );
// When autoevent signals the second time, dispose
// The timer.
Autoevent. waitone ( 5000 , False );
Statetimer. Dispose ();
Console. writeline ( " \ Ndestroying timer. " );
}
}
Class Statuschecker
{
Private Int Invokecount;
Private Int Maxcount;
Public Statuschecker ( Int Count)
{
Invokecount = 0 ;
Maxcount = Count;
}
// This method is called by the timer delegate.
Public Void Checkstatus (Object stateinfo)
{
Autoresetevent autoevent = (Autoresetevent) stateinfo;
Console. writeline ( " {0} checking status {1, 2 }. " ,
Datetime. Now. tostring ( " H: mm: Ss. fff " ),
( ++ Invokecount). tostring ());
If (Invokecount = Maxcount)
{
// Reset the counter and signal main.
Invokecount = 0 ;
Autoevent. Set ();
}
}
}
Finally, in order to apply knowledge to practice, the Timer class is used to implement a smallProgram:
This is indeed the case. I feel that only by writing code can I learn the knowledge well. This is the biggest achievement.