application.doevents Application of C # Learning notes

Source: Internet
Author: User

Summary in Visual Studio: processes all Windows messages currently in Message Queuing.

Hand over CPU control to allow the system to process all Windows messages in the queue
For example, in a large computation cycle, adding application.doevents can prevent the interface from stopping responding.


Because the WinForm message loop is a thread to handle, if one of your operations is time-consuming, then the message is processed so that you can continue when the time-consuming operation is done. The Application.doevents method is to allow you to invoke it within the time-consuming operation and to process messages in the message queue.

Like a mouse mouse click is a Windows message, if the time-consuming operation has been done, then the interface is like a deadlock.

Remember the first time to use Application.doevents () is to load a large amount of data can have a data loading hint, not the system appears suspended animation phenomenon, then did not go deep to study his principle is how, The results have been used in many places application.doevents (), today saw some articles on this, know I used to be inappropriate, some places need to use caution

Application.doevents ().
First of all, we look at the larger cycle of the program, its role is good, a real-time response to the effect, such as:

for (int q = 0; q < 1000000; q++)
{
TextBox1.Text = Q.tostring ();
Application.doevents ();//real-time response to values in text boxes
}

If not added DoEvents, because the cycle time will be longer than the state of suspended animation, and the program can not handle other events. And if add DoEvents words will be the value of the text box in real-time response to the user to bring a better user experience, but DoEvents also brought about the efficiency of the problem, processing the same event called the DoEvents after the efficiency of several times, which is why the use of caution. Here is a test I made:

private void Button1_Click (object sender, EventArgs e)
{
Expendtime.start ();
for (int q = 0; q < 100000; q++)
{
TextBox1.Text = Q.tostring ();
Application.doevents ();
}
Label2. Text = Expendtime.computertime ();//calculation time-consuming
}

private void Button2_Click (object sender, EventArgs e)
{
Expendtime.start ();
for (int q = 0; q < 100000; q++)
{
TextBox2.Text = Q.tostring ();
}
Label3. Text = Expendtime.computertime ();//calculation time-consuming
}


Execution time-consuming comparisons:
The efficiency is very low from the cycle of larger data, so if you can not call DoEvents, try not to. It can also be handled in other ways, such as multi-threaded asynchronous calls.

application.doevents Application of C # Learning notes

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.