System.Windows.Forms.Timer
Form-based applications
Blocking synchronization
Single Thread
A timer with a longer processing time results in a significant timing error.
System.Timers.Timer
Service-based
Non-blocking asynchronous
Multithreading
/// <summary> ///Windows Timers/// </summary>System.Windows.Forms.Timer _wtimer; /// <summary> ///Application Generation Timers/// </summary>System.Timers.Timer _ttimer; Private voidForm1_Load (Objectsender, EventArgs e) {_wtimer=NewSystem.Windows.Forms.Timer (); _wtimer.interval= -;//Set time interval 500 milliseconds_wtimer.tick + =_wtimer_tick; _wtimer.start ();//Start Timer_ttimer=NewSystem.Timers.Timer (); _ttimer.interval= -;//Set time interval 500 milliseconds_ttimer.elapsed + =_ttimer_elapsed; //_ttimer.start (); } void_ttimer_elapsed (Objectsender, System.Timers.ElapsedEventArgs e) {Print ("_ttimer_elapsed"+_count. ToString ()); Thread.Sleep ( -);//Sleep 2 seconds_count++; } void_wtimer_tick (Objectsender, EventArgs e) {Print ("_wtimer_tick"+_count. ToString ()); Thread.Sleep ( -);//Sleep 2 seconds_count++; } Private voidPrint (stringmsg) { if( This. invokerequired) { This. BeginInvoke (Action)Delegate() {Textbox1.appendtext (msg+"\ r \ n"); }); } Else{textbox1.appendtext (msg+"\ r \ n"); } }
when the _wtimer.start () is started, the output results. The _wtimer_tick sleeps for 2 seconds to block the main thread, causing the program to feign death for 2 seconds. And the event does not finish without processing the next time, so the _count results are output only once at a time.
When _ttimer.start () is started, a thread is added every 500 milliseconds. The next thread is automatically generated as soon as the asynchronous arrival interval occurs, regardless of whether the previous thread has finished processing.
So the result of _count will have multiple outputs.
If you want to resolve system.timers not to the time interval to generate only one thread or the last time that you have not finished processing, you can add _ttimer.stop () before processing, and then start _ttimer.start () again after completion.
Brief introduction of the difference between System.Windows.Forms.Timer and System.Timers.Timer usage