Timer thread timer in DotNet

Source: Internet
Author: User

 
You need to use the Close and Dispose methods to destroy resources. Note: Once the server timer object does not have any reference, the garbage collector recycles the object. Therefore, you need to use GC before the reference is invalid. the KeepAlive method does not recycle the instance. We recommend that you declare the server timer at the class level or higher to prevent this problem;
Server timers can only be applied to. NET Framework and are not supported by. NET Compact Framework (handheld devices) and XNA Framework (game development. The first two timers (Windows Timer and server Timer) of the System. Threading. Timer class (thread Timer) both inherit the Component class and can be inherited again as the parent class. The thread timer is a more lightweight timer. It is sealed and its declaration is as follows: [ComVisible (), HostProtection (SecurityAction. linkDemand, callback =, ExternalThreading = Timer (TimerCallback callback, state, dueTime, Timer (TimerCallback callback, state, dueTime, Timer (TimerCallback callback, Timer (TimerCallback callback, state, dueTime, Change (dueTime, TimerSetup (TimerCallback callback, Although the state, dueTime, period, constructor has five reloads, the first one (10th rows declared in the code above) is not recommended because it only works on the. NET Framework. This constructor only needs to specify a TimerCallback callback, while several other constructor functions have three other parameters. The significance of the four parameters is as follows: 1st parameters, TimerCallback callback. TimerCallback is a delegate that processes timer calls. It enables the timer to execute the method we write after the time, just like the so-called events in the first two timers, but it is implemented by the delegate callback method here. However, it should be clear that the delegate method is not executed on the thread where Timer is created, and it will be executed in a separate thread pool thread provided by the system. Therefore, to access the objects in the Timer thread in this method, you need to define another delegate in the thread for creating the Timer. After the thread is synchronized, access the object through this delegate. 2nd parameters, the object passed by the callback method. It can be null, mainly because we do not need to have an object. 3rd parameters, start time. This is the amount of latency before the first callback. You can use the integer value type, in milliseconds. 0 indicates immediate start, and Timeout. Infinite (that is, the preceding five constructors are not listed. The first constructor has only the first parameter. The other three parameters are set to null, Timeout. Infinite, and Timeout. Infinite.
The Change method has four overloading methods. They can be used to modify the timer start time and cycle time at any time. Their parameters are the same as those of the constructor's 3rd and 4 parameters. A very practical example is to pause the timer's periodic callback: Change (
 
 

If the dueTime is zero (0), the call back method is immediately called. If dueTime is used, the call-back method is never used. The timer is disabled. If you want to use it again, call and specify a positive value for dueTime.

 

The period is zero (0) or, And the dueTime is not Infinite, so it is only called a call-back method once. The scheduled operation of the timer will be disabled. If you want to use it again, call and specify a positive value for period.

 

Although this example does not demonstrate the method of Dispose with signals, MSDN cleverly uses the AutoResetEvent object as the parameter entrusted by the timer to interact with the Main thread, read the following line of code and comments carefully. You can also master the usage of thread timers and AutoResetEvent classes. [Example 1]

 

          AutoResetEvent autoEvent =  AutoResetEvent(         StatusChecker   statusChecker =  StatusChecker(         TimerCallback timerDelegate = , DateTime.Now.ToString(         Timer stateTimer =  Timer(timerDelegate, autoEvent, ,          autoEvent.WaitOne(,          stateTimer.Change(,          autoEvent.WaitOne(,  StatusChecker(= = count;      =), (++(invokeCount ==             invokeCount   = 
           _interval =         =             _timer =              Rectangle workArea =             StartPosition ==  Point(workArea.Right - .Width, workArea.Bottom -  MessageWindow( caption, ==                                                             MessageWindow( caption,  text, =               NativeMethods.AnimateWindow(.Handle, , NativeConstants.AW_BLEND +         TimerTick( (=            (_timer != .Handle, , NativeConstants.AW_BLEND +

 

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.