Application.doevents () Notes

Source: Internet
Author: User

MSDN Notes:
When you run a Windows form, it creates a new form, and then the form waits for the event to be processed. The form handles all the code associated with the event each time the event is processed. All other events are waiting in the queue. When the code handles the event, the application does not respond. For example, if you drag a window over window B, the b window will not redraw.
If you call DoEvents in your code, your application can handle other events. For example, if you have a form that adds data to a ListBox and adds DoEvents to your code, the form will be redrawn when you drag another window onto your form. If you remove DoEvents from your code, your form will not redraw until the button's Click event handler finishes executing. For more information about messages, see user input in Windows forms.

Example:
Write a program, do a loop output text example, do not add application.doevents () The value only shows the end of the output loop results, if added can be real-time output. This statement is determined to affect the efficiency of the program.

 private  void  button1_click (object   sender, EventArgs e) { long  t1 = System.DateTime.Now.Ticks;  for  (int  i = 0 ; I < 10000 ; I++ = I.tost                Ring ();            System.Windows.Forms.Application.DoEvents ();  long  T2 = System.DateTime.Now.Ticks            ; TextBox2.Text  = (T2- T1).        ToString (); }

Contains System.Windows.Forms.Application.DoEvents (); TextBox2.Text is 23471342

 Private voidButton2_Click (Objectsender, EventArgs e) {            LongT1 =System.DateTime.Now.Ticks;  for(inti =0; I <10000; i++) {TextBox3.Text=i.tostring (); //System.Windows.Forms.Application.DoEvents ();            }            Longt2 =System.DateTime.Now.Ticks; Textbox4.text= (T2-t1).        ToString (); }

does not contain System.Windows.Forms.Application.DoEvents (); TextBox2.Text is 10900624

So the advantage of DoEvents is that it can bring a good user experience to users, and the disadvantage is to reduce the efficiency of the program.

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.