Processes all Windows messages currently in the message queue.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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 in your code, your application can handle the other events. For example, if you have a form that adds data to a ListBox and add to your code, your form repaints when another window is dragged over it. If you remove 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 User Input in Windows Forms.
Unlike Visual Basic 6.0, the method does not call the Thread.Sleep method.
Typically, you use this method in a loop to process messages.
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 Model (APM).
The following code example demonstrates using the method. When the example runs, a user can select graphics files from an OpenFileDialog. The selected files are displayed in the form. The method forces a repaint of the form for each graphics file opened. To run this example, paste the following code in a form containing a PictureBox named PictureBox1, an OpenFileDialog named OpenFileDialog1, and a button named fileButton. Call the InitializePictureBox and InitializeOpenFileDialog methods from the form's constructor or Load method.
In Visual Studio, if you add an OpenFileDialog to your form by using a drag operation, you will have to modify the following InitializeOpenFileDialog method by removing the line that creates a new instance of OpenFileDialog.
The example also requires that the Control.Click event of the Button control and the FileOk event of the OpenFileDialog are connected to the event handlers defined in the example. When the example is running, display the dialog box by clicking the button.
To run this example paste the following code in a form containing a PictureBox named PictureBox1, an OpenFileDialog named OpenFileDialog1, and a button named fileButton. Call the InitializePictureBox and InitializeOpenFileDialog methods from the form's constructor or Load method. When the example is running, display the dialog by clicking the button.
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.