Turn from: http://blog.sina.com.cn/s/blog_3f0612650100grr6.html
The use of the timer timer in System.thread.
Timer class: Set up a timer that executes the user-specified function at timed intervals.
When the timer is started, the system will automatically create a new thread that executes the user-specified function.
Initializes a Timer object:
Timer timer = new Timer (timerdelegate, s,1000, 1000);
First parameter: Specifies a TimerCallback delegate that represents the method to be executed;
Second parameter: An object that contains the information to be used by the callback method, or a null reference;
Third parameter: Delay time-the time from which the timing begins is now, in milliseconds, and specifies "0" to start the timer immediately;
Fourth parameter: Timer Interval--After the timer starts, every so long time, the method represented by TimerCallback will be called once, in milliseconds. Specifies that timeout.infinite can be disabled for periodic termination.
Timer.change () Method: Modify the Timer settings. (This is a method of parameter type overloading)
Use Example: Timer. Change (1000,2000);
using System; using System.Threading;
Namespace Threadexample {class Timerexamplestate {public int counter = 0;
Public Timer TMR;
Class App {public static void Main () {timerexamplestate s = new Timerexamplestate ();
Creates a proxy object timercallback that will be periodically invoked timercallback Timerdelegate = new TimerCallback (checkstatus);
Create a timer with a time interval of 1s timer timer = new Timer (timerdelegate, s,1000, 1000);
S.TMR = timer;
The main thread stops to wait for the timer object to terminate while (S.TMR!= null) thread.sleep (0);
Console.WriteLine ("Timer example done.");
Console.ReadLine (); //Below is the method that is timed to invoke the static void CheckStatus (Object state) {timerexamplestate s = (timerexamplestate) state; S.counte
r++;
Console.WriteLine ("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, S.counter); if (S.counter = = 5) {//Changes the time interval (S.TMR) using the Change method.
Change (10000,2000);
Console.WriteLine ("Changed");
} if (S.counter = N) {Console.WriteLine ("Disposing of Timer"); S.tmr.dispose (); s.tmr = null;} }
}
}