Export (0) Print
Expand All

WindowsFormsApplicationBase.DoEvents Method

Processes all Windows messages currently in the message queue.

Namespace:  Microsoft.VisualBasic.ApplicationServices
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

'Declaration
Public Sub DoEvents

The My.Application.DoEvents method allows your application to handle other events that might be raised while you code runs. The My.Application.DoEvents method has the same behavior as the DoEvents method.

When you run a Windows Forms application, it creates a new form, which then waits for events to be handled. Each time the form handles an event, such as a button click, 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 My.Application.DoEvents in your code, your application can handle the other events. For example, if your code adds data to a ListBox in a loop, and after each step of the loop it calls My.Application.DoEvents, your form repaints when another window is dragged over it. If you remove My.Application.DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.

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

NoteNote

The My.Application.DoEvents method does not process events in exactly the same way as the form does. Use multithreading to make the form directly handle the events. For more information, see Multithreaded Applications (C# and Visual Basic).

Caution noteCaution

If a method that handles a user interface (UI) event calls the My.Application.DoEvents method, the method might be re-entered before it finishes. This can happen because the My.Application.DoEvents method processes Windows messages, and Windows messages can raise events.

The following table lists an example of a task involving the My.Application.DoEvents method.

To

See

Allow a form to respond to UI input while busy

Walkthrough: Handling Events (Visual Basic)

Availability by Project Type

Project type

Available

Windows Forms Application

Yes

Class Library

No

Console Application

No

Windows Forms Control Library

No

Web Control Library

No

Windows Service

No

Web Site

No

This example uses the My.Application.DoEvents method to allow the UI for TextBox1 to update.

Private Sub TestDoEvents()
    For i As Integer = 0 To 10000
        TextBox1.Text = i.ToString
        My.Application.DoEvents()
    Next 
End Sub

This code should be in a form that has a TextBox1 component with a Text property.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft