Virtual Developer Workshop: Containerized Development with Docker

This class displays a translucent, shadowed modal progress dialog. The process whose progress is to be monitored is started as a new thread, allowing the progress sphere to update smoothly. If enabled, the user is given the ability to terminate the process with the ESC key. The code runs on Windows 2000 and later.

1. Structure

All initialisation is performed in the constructor:

CDlgProgress(HWND hParentWnd, PROGRESSFUNC hThreadProc,
             const wchar_t* strTitle, bool bAllowCancel = false);

The parameters in detail:

  • hParentWnd takes a handle to the calling window
  • PROGRESSFUNC supplies the name of the user function that contains the code whose progress is to be monitored
  • strTitle is the text that will display at the top of the window, during the entire execution
  • bAllowCancel tells the class whether to allow premature termination by the user

This method checks whether the progress funtion should continue executing:

bool Continue();

The following function sets the status message of the progress dialog:

void SetProgress(CString strText, double dPos);
  • strText is the status message
  • dPos is the percent completed

This one returns the handle of the dialog:

HWND GetWindowHandle();

The progress function needs to have the following form:

bool ProgressFunction(CDlgProgress* pDlgProgress);

2. Usage

  1. Do the inclusion:
    #include "CDlgProgress.h"
  2. Construct the CDlgProgress object; for example:
    CDlgProgress dlg(m_hWnd, ProgressFunction,
                     L"Lengthy operation being performed...",
  3. Write a progress function:
    bool ProgressFunction(CDlgProgress* pDlgProgress)
       for(int i = 0; i < 5000; i++)
             return 1;
          CString strMessage;
          strMessage.Format(L"Current number is %i", i);
          pDlgProgress->SetProgress(strMessage, (double)i/50.0f);
       return 0;



  • Good Start.

    Posted by AliRafiee on 07/24/2009 10:50pm

    This is a nice control, but has a few drawbacks. It would be really nice if it was scalebale, since the size is fixed it would be difficult to use. The other thing is the fact that you require the user to have a static call back which will make it difficult to program around. It would be nice it your control would just plug into any dialog, that way the user can put their long process into a worker thread, and simply show a dialog with your control in it while the thread is working on the long process. I guess All of this can be down by the readers of the article in a few hours, but again would have been nice if it was there.

    • Good Start.

      Posted by tari on 07/25/2009 11:12am

      Hello Ali, You have a point here, but this was intended more as a demo rather than a complete solution. (Un)fortunately I've completely moved on to Linux since I wrote it, so it's up to you and the community to shape the code to suit your specific needs. Feel free to copy/paste parts of the code and experiment, you can even alter it and post it back here, if you wish so. The project is 100% my coinage, I hold no copyright - you can freely take it apart and use it any way you seem appropriate.

  • Unhandled exception

    Posted by glenn.wasserman on 03/22/2007 03:47pm

    I'm using this in an existing MFC app and I seem to get random unhandled exceptions...where the debugger breaks inside AfxCallWndProc and the pWnd pointer passed into the routine is null. Mostly these exceptions happen when I shutdown the app but they also can happen when I execute the code that uses this module. Note that I can't get the exception to happen on a debug build - it only happens in my "release" build

    • Unhandled exception

      Posted by glenn.wasserman on 03/27/2007 03:40am

      I figured out the problem - in the code that detects that ESC was hit, it was calling WaitForSingleObject with a 5 second wait, after which it was calling EndDialog. What I found was that sometimes, a short time after this, my code was returning and then your code was calling EndDialog a second time. So I eliminated the EndDialog in the callback and rely on my code returning back into your code to call EndDialog just once.

    • Re: Unhandled exception

      Posted by tari on 03/26/2007 12:45pm

      Hello Glenn,
      What you're describing here could be caused by almost anything, AfxCallWndProc can break for various reasons. Have you tried commenting out the ProgressSphere code from your program and see if you still get errors? If all else fails, you could send me your source code to have a look at (unless it's a top secret missile guiding application, of course).

  • demo application

    Posted by mop65715 on 07/07/2006 11:29pm

    I'm executing the demo application and it appears that once the application starts there's no way to stop the application - short of using task manager. Am I correct?

    • demo application

      Posted by tari on 07/08/2006 10:15am

      Hello, Michele. If cancel is enabled in the constructor, you should be able to stop it by pressing the ESC key when the window has focus.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date