Brief introduction of the difference between System.Windows.Forms.Timer and System.Timers.Timer usage

Source: Internet
Author: User

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

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.