Click here to Skip to main content
13,446,762 members (44,192 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 21 Dec 2012

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

, 21 Dec 2012
Rate this:
Please Sign up or sign in to vote.
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.


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.


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
           //Process messages in the queue
           //MessageFilter desregistration
       /// <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

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.


No changes yet.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Spain Spain
Programming since the times of good old Spectrum.
Windows developer (from C++/COM to C# now).

You may also be interested in...


Comments and Discussions

Questionstill dangerous Pin
VV1215214712-May-16 4:11
memberVV1215214712-May-16 4:11 
AnswerRe: still dangerous Pin
dromanol12-May-16 4:46
memberdromanol12-May-16 4:46 
GeneralDangerous Pin
Maxence Delannoy28-Dec-13 21:57
memberMaxence Delannoy28-Dec-13 21:57 
GeneralRe: Dangerous Pin
dromanol1-Jan-14 20:23
memberdromanol1-Jan-14 20:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180318.2 | Last Updated 21 Dec 2012
Article Copyright 2012 by dromanol
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid