1. system. Threading. Timer
Code
// Http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
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 (1, 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 ();
}
}
}