Small Interval between system. Timers. timer and system. Threading. Timer

Source: Internet
Author: User
Design a program to check the status every 20 ms. timers. during the timer test, it was found that the time for the previous timer function calls was the same (no problem occurred when the interval was changed to more than 1 s ), lock is also used (maybe I don't quite use lock ).
Changed to system. Threading. Timer. The test found that:
The timer of Threading and timers has this problem at a small interval. After analysis, it is preliminarily determined that it is the interval before the first run.
Timers cannot set the interval before the first start. Therefore, the interval before the first entry is also 20 ms when the interval of 20 ms is set.
You can set the interval before the first startup for threading. If you set a large interval to start later, you will not be able to start multiple threads at the same time during the first running.
Timerclose = new system. Threading. Timer (New timercallback (timercall), null, 20, 20); // enter
Timerclose = new system. Threading. Timer (New timercallback (timercall), null, 1000, 20); // enter normally

Code used for testing: using system;
Using system. Collections. Generic;
Using system. Windows. forms;
Using system. Timers;
Using system. Threading;

Namespace windowsapplication3
{
Static class Program
{
/** // <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
// System. Threading. Timer thrtimer = new system. Threading. Timer ();




System. Timers. Timer timer = new system. Timers. Timer ();
Timer. elapsed + = new elapsedeventhandler (timer_elapsed );

Timer. autoreset = true;
Timer. interval = 20;

System. Threading. Timer timerclose;
// Solve the problem of simultaneous function access by multiple threads when the timer calls the function for the first time
// The timer of Threading and timers both have this problem. After the problem is analyzed, it is preliminarily determined that it is the interval before the first run.
// Timers cannot set the interval before the first start. Therefore, the interval before the first entry is also 20 ms when the interval is set to 20 ms.
// For threading, you can set the interval before the first startup. If a large interval is set and then the thread starts at the same time during the first running.
// Timerclose = new system. Threading. Timer (New timercallback (timercall), null, 1000, 20 );
// Timerclose = new system. Threading. Timer (New timercallback (timercall), null, 20, 20 );
// Timer. Start ();


Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Application. Run (New form1 ());
}
Public static void timer_elapsed (Object sender, elapsedeventargs E)
{
Console. Out. writeline ("system:" + datetime. Now + "" + datetime. Now. millisecond + "" + datetime. Now. timeofday. totalmilliseconds );

}
// Object oo = new object ();
// Static int intimer = 0;
Public static void timercall (Object OBJ)
{

// Timerclose. Dispose ();
// Lock (this)
// If (interlocked. Exchange (ref intimer, 1) = 0)
{
// Console. Out. writeline (environment. tickcount );
Console. Out. writeline ("threading:" + datetime. Now + "" + datetime. Now. millisecond + "" + datetime. Now. timeofday. totalmilliseconds );
// This. Close ();
// Interlocked. Exchange (ref intimer, 0 );
}

}




}
}

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.