C # checkstatus () method

Source: Internet
Author: User
Tags terminates
    Here we will introduce the C # checkstatus () method and call the timer. Dispose () method to delete the timer object. The main thread jumps out of the loop and terminates the program.

     

    C # is a common language. Here we mainly introduce the C # checkstatus () method, including setting a timer and regularly executing user-specified functions.

     

    Timer class: sets a timer to regularly execute user-specified functions.

     

    After the timer starts, the system automatically creates a new thread to execute the user-specified function.

     

 
 
  1. Timer timer = new timer (timerdelegate, S, 1000,100 0 );
  2. // The first parameter: Specifies the timercallback delegate, indicating the method to be executed;
  3. // The second parameter: an object that contains the information to be used by the callback method, or an empty reference;
  4. // The third parameter: Delay Time, in milliseconds,
    0 indicates that the timer is started immediately;
  5. // The fourth parameter: the timer interval. After the timer starts,
    Timercallback indicates that the method will be called once in milliseconds.
    The specified Timeout. Infinite can disable periodic termination.

Timer. Change () method: Modify the timer settings. (This is a method to overload the parameter type) Example: timer. Change );

 

Example of a Timer Program:

 

 
 
  1. Using system;
  2. Using system. Threading;
  3.  
  4. Namespace threadexample
  5. {
  6. Class timerexamplestate
  7. {
  8. Public int counter = 0;
  9. Public timer TMR;
  10. }
  11.  
  12. Class app
  13. {
  14. Public static void main ()
  15. {
  16. Timerexamplestate S = new timerexamplestate ();
  17.  
  18. // Create the proxy object timercallback, which will be called regularly
  19. Timercallback timerdelegate = new timercallback (checkstatus );
  20.  
  21. // Create a timer with an interval of 1 s
  22. Timer timer = new timer (timerdelegate, S, 1000,100 0 );
  23. S. TMR = timer;
  24.  
  25. // The main thread stops and waits for the termination of the timer object
  26. While (S. TMR! = NULL)
  27. Thread. Sleep (0 );
  28. Console. writeline ("timer example done .");
  29. Console. Readline ();
  30. }
  31.  
  32. // The following method is called regularly
  33. Static void checkstatus (object state)
  34. {
  35. Timerexamplestate S = (timerexamplestate) State;
  36. S. Counter ++;
  37. Console. writeline ("{0} checking status {1}.", datetime. Now. timeofday, S. Counter );
  38.  
  39. If (S. Counter = 5)
  40. {
  41. // The change method is used to change the time interval.
  42. (S. TMR). Change (random, 2000 );
  43. Console. writeline ("changed ");
  44. }
  45.  
  46. If (S. Counter = 10)
  47. {
  48. Console. writeline ("disposing of timer ");
  49. S. TMr. Dispose ();
  50. S. TMR = NULL;
  51. }
  52. }
  53. }
  54. }

The program first creates a timer, which will call the C # checkstatus () method once every one second after the creation of 1 second. After five calls () the time interval is modified to 2 seconds, and the task starts again after 10 seconds. When the Count reaches 10 times, the timer object is deleted by calling the timer. Dispose () method, 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.