C # timer Application

Source: Internet
Author: User

simple timer applications are not complex. You can set several parameters based on your own situation. The following Code :

Private voidRun (){TimercallbackTCB =NewTimercallback(Executerun); // The timer starts to run after 5 seconds (duetime), and then runs once every 1 second (period ).TimerTimer =NewTimer(TCB,Null, 5000,100 0 );}Private voidExecuterun (ObjectOBJ ){Console. Writeline ("Timer is running"+Datetime. Now. tostring ());}
 
In our scheduledjob project, some jobs adopt this method: Execute at a specified time;
However, in some cases, the execution of a job is not so regular. It may be executed once on the first day of every month, maybe on Monday, or at three o'clock every day, at three o'clock P.M,
 
At this time, you have to use another method for Timer: after a job is executed, calculate the time point to be executed next time based on the specified file, calculate the current and next execution time as duetime,
 
Change the duetime of timer. The following code demonstrates how to use the change method to change duetime.
 Using System; Using System. Collections. Generic; Using System. Threading; Namespace Filesystemwatcherdemo { /// <Summary> /// demo timer duetime change    /// </Summary>  Class  Timerdemo { Dictionary < String , Timer > Timerpool; Private int Duttime = 1; Static void Main ( String [] ARGs ){ Timerdemo Demo = New  Timerdemo (); Demo. beginrun (); 
             Console . Read ();} Private void Beginrun () {timerpool = New  Dictionary < String , Timer > (); For ( Int I = 1; I <= 1; I ++ ){ Timercallback TCB = New  Timercallback (Execute ); // Timeout. Infinite specifies that the timer starts to run after duetime and only runs once. to execute it again, use the change method to re-specify the duetime  Timer Timer =New  Timer (TCB, I. tostring (), getduetime (duttime ), Timeout . Infinite); timerpool. Add (I. tostring (), timer );}} Private void Execute ( Object OBJ ){ String Ep = OBJ As string ; If (Ep! = Null ){ Console . Writeline ( "Timer" + EP +"Is running" + Datetime . Now. tostring ()); Timer Timer = timerpool [EP]; timer. Change (getduetime (duttime ++ ), Timeout . Infinite ); // Use the change method to re-specify duetime }} Private long Getduetime ( Int I ){ Return  Convert . Toint64 (I * 1000 );}}}

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.