Application.DoEvents的用法

itscrambler發表於2011-10-11

Application.DoEvents 方法

當執行 Windows 窗體時,它將建立新窗體,然後該窗體等待處理事件。該窗體在每次處理事件時,均將處理與該事件關聯的所有程式碼。所有其他事件在佇列中等待。在程式碼處理事件時,應用程式並不響應。例如,當將另一視窗拖到該視窗前面時,該視窗不重新繪製。

如果在程式碼中呼叫 DoEvents,則應用程式可以處理其他事件。例如,如果向 ListBox 新增資料的窗體,並將 DoEvents 新增到程式碼中,那麼當將另一視窗拖到該窗體上時,該窗體將重新繪製。如果從程式碼中移除 DoEvents,那麼在按鈕的單擊事件處理程式執行結束以前,改窗體不會重新繪製。

通常,在迴圈中使用該方法來處理訊息。

參考:

When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.

If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing. For more information on messaging, see .

Unlike Visual Basic 6.0, the DoEvents method does not call the method.

Typically, you use this method in a loop to process messages.

Caution

Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute. This can cause your application to exhibit unexpected behaviors that are difficult to debug. If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread. For more information about asynchronous programming, see .

Caution
Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute. This can cause your application to exhibit unexpected behaviors that are difficult to debug. If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread. For more information about asynchronous programming, see Asynchronous Programming Overview.

Caution

Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute. This can cause your application to exhibit unexpected behaviors that are difficult to debug. If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread. For more information about asynchronous programming, see .

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26273052/viewspace-1055609/,如需轉載,請註明出處,否則將追究法律責任。