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 );
}
}
}
}