from: http://kb.cnblogs.com/page/42532/
Timer class: Set a timer to execute the user-specified function on a timed basis.
After the timer starts, a new thread is automatically created to execute the user-specified function.
Initialize a Timer object:
Timer timer = new Timer (timerdelegate, s,1000, 1000);
First parameter: The TimerCallback delegate is specified, indicating the method to be executed;
Second argument: An object containing the information to be used by the callback method, or a null reference;
Third parameter: Delay time-the time at which the timing starts, in milliseconds, specified as "0", to start the timer immediately;
Fourth parameter: Timer Interval--After the start of the timer, every so long period of time, the method that TimerCallback represents will be called once, the unit is also milliseconds. Specifies that timeout.infinite can disable periodic termination.
Timer.change () Method: Modify the Timer settings. (This is a method of parameter type overloading)
Use Example: Timer. Change (1000,2000);
Example of a program for the Timer class (Source: MSDN):
usingSystem;usingSystem.Threading;namespacethreadexample{classTimerexamplestate { Public intCounter =0; PublicTimer TMR; } classApp { Public Static voidMain () {timerexamplestate s=Newtimerexamplestate (); //creates a proxy object TimerCallback, which is called periodically by the agentTimerCallback timerdelegate =NewTimerCallback (checkstatus); //Create a timer with a time interval of 1sTimer timer =NewTimer (Timerdelegate, S, +, +); S.TMR=timer; //The main thread pauses to wait for the timer object to terminate while(S.TMR! =NULL) Thread.Sleep (0); Console.WriteLine ("Timer example Done."); Console.ReadLine (); }//here are the methods that are called periodically Static voidcheckstatus (Object state) {timerexamplestate s=(timerexamplestate) state; S.counter++; Console.WriteLine ("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, S.counter); if(S.counter = =5) { //changing the time interval using the Change method(S.TMR). Change (10000, -); Console.WriteLine ("changed"); } if(S.counter = =Ten) {Console.WriteLine ("Disposing of Timer"); S.tmr.dispose (); S.TMR=NULL; } }}}
View Code
The program first creates a timer, which begins to call the CheckStatus () method every 1 seconds after the creation of 1 seconds, and after 5 calls, modifies the time interval to 2 seconds in the CheckStatus () method, and specifies to start again after 10 seconds. When the count reaches 10 times, the call Timer.dispose () method removes the timer object, and the main thread jumps out of the loop and terminates the program.
------------------------>>>