Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Repainting WinForms windows safely inside a processing loop (safe DoEvents)

0.00/5 (No votes)
21 Dec 2012 1  
This example illustrates the correct use of Application.DoEvents() in order to allow a window to repaint (or handle the desired messages) while its thread is busy doing heavy processing.

Introduction

This example illustrates the correct use of Application.DoEvents() in order to allow a window to repaint (or handle the desired messages) while its thread is busy doing heavy processing.

Background

Calling Application.DoEvents() used to be the easy way of making your window repaint, but this method is really dangerous, because it not only allows the window to repaint, but also processes all the events the user issues to the window, such as button presses etc., which can lead to unwanted behavior in the form of function reentrancy or unwanted event handler execution, leading to malfunction and possible crashes.

Using the code

The code comes in the form of a utility class with a function called DoPaintEvents().

The solution is based on the use of a IMessageFilter that decides which messages will do execution and which don’t, and it’s pretty straightforward and easy to modify to fit other user needs.

namespace System.Windows.Forms
{
    /// <summary> System.Windows.Forms utilities </summary>
    public static class WinFormUtils
    {
       /// <summary> Processes all Paint events only </summary>
       public static void DoPaintEvents()
       {
           //MessageFilter registration
           Application.AddMessageFilter(PaintMessageFilter.Instance);
           //Process messages in the queue
           Application.DoEvents();
           //MessageFilter desregistration
           Application.RemoveMessageFilter(PaintMessageFilter.Instance);
       }
 
       /// <summary> Custom message filter </summary>
       private class PaintMessageFilter : IMessageFilter
       {
           static public IMessageFilter Instance = new PaintMessageFilter();
 
           #region IMessageFilter Members
 
           /// <summary> Message filter function </summary>
           public bool PreFilterMessage(ref System.Windows.Forms.Message m)
           {
                   return (m.Msg != 0x000F); //WM_PAINT -> we only let WM_PAINT messages through
           }
 
           #endregion
       }
    }
}

Now, if you walk into a processing loop inside a button click message handler you will be able to call WinFormUtils.DoPaintEvents() in order to make your window repaint itself safely.

private void button1_Click(object sender, EventArgs e)
{
   bool done = false;
   while(!done) //This loop will leave your window stuck
   {
        //Process something
        done = DoSomeHeavyStuff();
        //Repaint the window
        WinFormUtils. DoPaintEvents ();
   }
}

Points of Interest

You can modify the PreFilterMessageFunction in order to let any message you need through, like min/max/restore events, for instance.

History

No changes yet.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here