Timer class in C #

Source: Internet
Author: User

· about the Timer class in C # there are three timer classes in C #
1. defined in system. windows.
2. defined in system. threading.
3. defined in system. timers.

system. windows. forms. timer is used in winform. It is implemented through the windows message mechanism. Similar to the timer control in VB or Delphi, it is implemented using the settimer API internally. Its main disadvantage is that the timing is inaccurate and there must be a message loop. The console application (console application Program ) cannot be used.
system. timers. timer and system. threading. timer is very similar, they are through. net thread pool implementation, lightweight, accurate timing, no special requirements for applications, messages. System. Timers. timer can also be used in winform to completely replace the above Timer control. Their disadvantage is that they do not support direct drag and drop and require manual encoding.

Example 1:
Use System. timers. timer class
system. timers. timer T = new system. timers. timer (10000); // instantiate the Timer class and set the interval to 10000 milliseconds.
T. elapsed + = new system. timers. elapsedeventhandler (theout); // execute the event when the arrival time is reached;
T. autoreset = true; // set whether to execute once (false) or always execute (true);
T. enabled = true; // whether to execute system. timers. timer. elapsed event;

Public void theout (Object source, system. timers. elapsedeventargs e)
{< br> MessageBox. show ("OK! ");
}< WBR>

Example 2: system. threading. timer class timercallback DeleGate

system. threading. timer is a timer that uses the callback method and serves by the thread pool. It is simple and does not require high resources.

If timer is used, it must be retained. If there is no reference to timer for any managed object, 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 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) to wait before the first execution of the method and the amount of time (time cycle) to wait for the subsequent execution ). 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 timer variable: private system. threading. timer timerclose;
(2) Add the following timer instantiation code after the above Automatic Code Execution:
// 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 indicates the method to be executed.
State: an object that contains the information used by the callback method, or a null reference (nothing in Visual Basic ).
duetime: the 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 call interval (in milliseconds ). 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.

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.