Application. DoEvents ()

Source: Internet
Author: User

Today, I am doing exercises based on a winform example in the book (in fact, I copied it while reading it ...) when you see Application. the DoEvents () method does not understand what the method is. If this method is not used, the window ui rendering may fail when the time-consuming operations are executed cyclically.

So I searched for it on the Internet, but those theories and concepts were a bit difficult for me. But now I just want to understand the role of this method, so I will give it a simple explanation according to my own understanding.

   btnCopy_Click(<FileInfo> fileList = (FileInfo file =  + txtTargetDir.Text +  + file.Name +                     file.CopyTo(txtTargetDir.Text +  += 

The above code is one of my exercises. The purpose is to change the text named "Done" to "Done" when you click the copy button.

What is the role of the method here, so I will comment it out and then run it. After I click copy, the window is like "dead", but the background file is indeed being copied. After the copy is complete, the text is changed to done.

Windows message system consists of three parts:

 

· Message queue. Windows can maintain a message queue for all applications. The application must obtain the message from the message queue and assign it to a window.

 

· Message loop. Through this loop mechanism, the application retrieves a message from the message queue, assigns it to an appropriate window, then searches for the next message from the message queue, and then assigns it to the appropriate window,.

 

· Window process. Each window has a window process to receive messages sent to the window. Its task is to obtain the message and then respond to it. A window is a callback function. After processing a message, it usually returns a value to Windows.

3. A message is responded to by a window from generation to generation. Five steps are involved:

1) An event occurs in the system.

2) Windows translates the event into a message and puts it in the message queue.

3) The application receives the message from the message queue and stores it in the TMsg record.

4) The Window process in which the application sends messages to an appropriate window.

5) The Window Process responds to the message and processes it.

Steps 3 and 4 constitute the message loop of the application. Message loops are often the core of Windows applications, because message loops allow an application to respond to external events. The task of message loop is to retrieve messages from the message queue and pass the messages to the appropriate window. If there are no messages in the message queue, Windows allows other applications to process their messages.

It's almost the same here. I haven't read any other materials. I can't remember what I can understand, so I won't mention it. So back to the original topic, what role does the Application. DoEvents () method play here?

In the above Code, when we click the copy button, Every time windows adds two messages to the current message queue in the loop, one is to change text, and the other is to copy a file. However, copying a file is obviously a time-consuming operation. Comment out Application. after the DoEvents () line of code, it is obvious from the results displayed in the running window that it first responds to the message of the copy file, then, after all the copy file operations are completed, we will be able to see the effect produced by the last = If we respond to the text change operation. Of course, during the copy process, we need to click the window or drag the window. These operations will be completed after the copy operation.

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.