The use of System.Thread.Timer timer _timer

Source: Internet
Author: User
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;} }
}
} 

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.