Experience in using System. Threading. Timer

Source: Internet
Author: User

System. Threading. Timer is a Timer that uses the callback method and serves by the thread pool thread. It is simple and does not require high resources.

"As long as you are using Timer, you must keep references to it.
"For any managed object, if there is no reference to Timer, the Timer will be reclaimed. Timer is recycled even if it is still active.
"When you no longer need a timer, use the Dispose method to release the resources held by the timer.

Use the TimerCallback delegate to specify the method to be executed by the Timer. The timer delegate is specified when the timer is constructed and cannot be changed. This method is not executed in the thread that creates the timer, but in the thread pool thread provided by the system.

When creating a timer, you can specify the amount of time (End Time) waiting before the first method is executed and the amount of time (cycle) waiting in the subsequent execution period ). You can use the Change method to Change these values or disable timers.

Demo application:
Application Scenario: After the windows form program automatically executes a job, it is expected that its windows form will be automatically disabled.

"Code Design:

(1) Declare the Timer variable first:
// Make sure to declare a local variable to keep reference to Timer; otherwise, it will be recycled by the garbage collector!
Private System. Threading. Timer timerClose;

(2) Add the following Timer instantiation code after the above Automatic Execution Code:
// Create a timer thread and start it
TimerClose = new System. Threading. Timer (new TimerCallback (timerCall), this, 5000, 0 );

// Timer constructor parameter description:
Callback: A TimerCallback delegate that indicates the method to be executed.
State: an object that contains the information to be used by the callback method, or a null reference (Nothing in Visual Basic ).
DueTime: The amount of time delay before calling callback (in milliseconds ). Specify Timeout. Infinite to prevent the timer from starting timing. Specify zero (0) to start the timer immediately.
Period: The interval (in milliseconds) between calls to callback ). The specified Timeout. Infinite can disable periodic termination.

(3) define the method to be executed by the TimerCallback delegate:
Private void timerCall (object obj)
{
TimerClose. Dispose ();
This. Close ();
}

Of course, in addition to using the TimerCallback delegation mechanism of the above System. Threading. Timer class, there should be many other methods.
In addition, it only demonstrates the simple application of the TimerCallback delegate.

Appendix about the article
When reading a piece of code about Design Pattern, we can see that the Timer class TimerCallback delegate is used and recorded along.

Reference:
1, MSDN, System. Threading. Timer class
 

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.