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.