C # Thread (vi): Timer

Source: Internet
Author: User

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.

------------------------>>>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.