Difference between three timers in FCL

Source: Internet
Author: User

FCL provides three timersProgramThey are not quite clear about the differences between the three timers.

Next we will analyze the three Timers:

System. Threading Timer class. This timer is the most commonly used timer. This timer is the best choice when we want to regularly execute background tasks on another thread.

The timer class of system. Windows. Forms builds an instance of this class and tells windows to associate the timer with the call thread. With the timer triggered, Windows inserts a timer message (wm_timer) into the thread message queue. The call thread must execute a message pump to extract messages and dispatch them to the desired callback method. Note that all these tasks are completed by one thread-the thread that sets the timer is the thread that executes the callback method. This also means that our timer method cannot be executed by multiple threads at the same time.

System. Timers Timer class the timer is basically a packaging of the Timer class of system. Threading. When the timer time expires, the CLR will add the event to the queue of the thread pool. The component class allows you to place these timer objects on the Microsoft Visual Studio design interface. In addition, the members of the system. Timers. Timer class are also a little different. This class was added to FCL several years ago, and Microsoft was still classifying thread processing and timer materials. This class may be removed so that everyone can use the Timer class of system. threading. In fact, we never use the system. Timer. Timer class, and I advise you not to use this class unless you want to use a timer on the design interface.

 

The following is an example of the Timer class of system. Threading. This class is frequently used:

 Using  System;  Using  System. Threading;  Namespace  Testtimer {  Public  Class  Program {  Public   Static   Void Main ( String  [] ARGs) {console. writeline (  "  Main thread: Starting Timer  "  );  //  Computeboundop is the executed function. Timer T = New Timer (computeboundop,5 , 0 , 2000  ); Console. writeline (  "  Main thread: working...  "  ); Thread. Sleep (  10000 ); //  Simulated operation (10 seconds) T. Dispose (); //  Cancel Timer  }  ///  <Summary>          ///  The signature of this method must match the timercallback delegate type.  ///   </Summary>          ///   <Param name = "state"> </param>          Private   Static   Void  Computeboundop (object state ){  //  This method is executed by threads in the thread pool. Console. writeline ( " In computeboundop: State = {0}  "  , State); thread. Sleep (  1000 ); //  Simulate other operations (1 second)  //  After this method is returned, the thread returns to the thread pool and waits for another task to be executed.  }}} 

 

When the timer object is garbage collected, CLR will cancel the timer so that the timer does not continue to be triggered. Therefore, when using the timer object, make sure that a variable references the timer object, to keep the timer object alive, otherwise the callback method will not be called.

Timer T = new timer (computeboundop, 5, 0, 2000 );CodeIn, computeboundop is the thread callback method, and 5 is the state parameter of the constructor. It allows us to pass the State data to the callback method. If there is no State data, it can pass null. 0 is the duetime parameter. The duetime parameter tells the CLR how many milliseconds it will wait to call the callback method for the first time. The value 0 indicates that the callback method is called immediately. 2000 is the period parameter, which indicates the interval between the callback method execution. Here we set 2000 milliseconds.

For more original content, visit my website (www.shuonar.com.

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.