Click here to Skip to main content
Click here to Skip to main content

Background Task Dialog

, 19 Apr 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
A wrapper for dialog boxes where you must do a long-time process but you don't want your application to appear to be HUNG.

Sample screenshot


Hi, my name is Mauro H. Leggieri, a Windows and Game programmer. I made a little thing that may be useful, and want to share with you.

I wrote a class named CBackgroundTaskDialog derived from the standard CDialog class. It is a wrapper for dialog boxes where you must do a long-time process, optionally showing the progress information, but you don't want your application to appear to be hung.

Using the code

The code simply creates a background thread and calls some member functions where you write your own code and have some members to control when to start/stop the background process.

To use the code in your project, first add the BackgroundTaskDialog.cpp and BackgroundTaskDialog.h files to your project.

Then, in the header file of the dialog you want to subclass, add the include "BackgroundTaskDialog.h" statement and replace all CDialog words with CBackgroundTaskDialog and add the following member declarations to the class:

class your_dialog_class : public CBackgroundTaskDialog
  // Class definition an so on...
  // Code that you must add to the class:

  void RunBackgroundTask();

After this, replace in the CPP file of the dialog all CDialog words with CBackgroundTaskDialog too and add the following members code:

void your_dialog_class_name::RunBackgroundTask()
  //Here you should add the code for the long-term process to do
  //At regular intervals you should call then 
  //CheckForBackgroundTaskAbort function
  //to check for an abort signal. If the function 
  //returns TRUE, usually simply return

To start the background process, you must use the StartBackgroundTask function. It returns a value of 1 if all is ok or less than 0 if an error occurs. The function is usually used in the OnInitDialog function.

BOOL your_dialog_class_name::OnInitDialog()
  // Code added by MFC AppWizard and your own code

  // Example code to add:

  if (StartBackgroundTask() < 0)
     PostMessage(WM_CLOSE, 0, 0);
     return TRUE;

  return TRUE;

At last, if you want to abort the background process, call the StopBackgroundTask function.

See the example project because it has more explanations.


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


About the Author

Mauro Leggieri
Web Developer
Argentina Argentina
My name is Mauro Leggieri. I am 30 year-old, married and have a child.
I am a system engineer (UTN university) and am being programming for more than 20 years from the C64 to the PC and some microcontrollers.
Mostly of my time, I program games for Windows.
Soon, my site

Comments and Discussions

QuestionCan we achieve this without a &amp;quot;Dialog&amp;quot; [modified] PinmemberIScribe3-Jun-07 15:25 
GeneralSome remarks Pinmembercristitomi22-Mar-07 5:54 
GeneralRe: Some remarks PinmemberMauro Leggieri22-Mar-07 9:51 
GeneralRe: Some remarks Pinmembercristitomi22-Mar-07 12:38 
GeneralRe: Some remarks PinmemberMauro Leggieri23-Mar-07 3:35 
GeneralRe: Some remarks PinmemberMember 369115-Mar-09 0:34 
Thank you for your improved codes.
- OnBtnStartStop();
- RunBackgroundTask();
// some modified and attached to use again later.
// 1. Removed: sleep();
// 2. Added: _T() to some omitted non-unicode string. (although it is unnecessary)
// 3. Changed: "GetDlgItem()->SetWindowText()" => SetDlgItemText()

void CBackgroundTaskDialogDemoDlg::OnBtnStartStop() 
    // Check if the background process is running    
    if (IsBackgroundTaskRunning() == FALSE)     
        // Start background process	
        if (StartBackgroundTask() < 0) 	
            SetDlgItemText(IDC_STC_PROGRESS, _T("Error: Cannot start background thread."));
        SetDlgItemText(IDC_BTN_STARTSTOP, _T("Stop"));
	// Abort background process	
	SetDlgItemText(IDC_STC_PROGRESS, _T("Thread was stopped."));
	SetDlgItemText(IDC_BTN_STARTSTOP, _T("Start"));
void CBackgroundTaskDialogDemoDlg::RunBackgroundTask()
    TCHAR buf[1024]={0,};
    int i=0;
    for (i=0; i < 500; i++)     
        // Check if I have to abort	
        if (CheckForBackgroundTaskAbort() != FALSE)	    
        // Update status text	
        _stprintf(buf, _T("Counting %ld of 500"), i+1);
        SetDlgItemText(IDC_STC_PROGRESS, buf);	
    //Update status text
    SetDlgItemText(IDC_STC_PROGRESS, _T("Thread has finished successfully."));
    SetDlgItemText(IDC_BTN_STARTSTOP, _T("Start"));

Taeha Kim

GeneralRe: Some remarks PinmemberMauro Leggieri15-Mar-09 10:23 
GeneralRe: Some remarks [modified] PinmemberMember 369115-Mar-09 15:25 
GeneralRe: Some remarks PinmemberMauro Leggieri16-Mar-09 7:15 
Questionwhat to do when using multiple base class? Pinmember_kane_27-Dec-05 8:21 
AnswerRe: what to do when using multiple base class? PinmemberMauro Leggieri27-Dec-05 8:50 
GeneralRe: what to do when using multiple base class? Pinmember_kane_29-Dec-05 7:04 
GeneralRe: what to do when using multiple base class? PinmemberMauro Leggieri29-Dec-05 8:49 
GeneralRC -4 after 85 DoModal()s PinsussAnonymous29-Jun-05 3:40 
GeneralRe: RC -4 after 85 DoModal()s PinmemberMX-Mauro29-Jun-05 12:36 
QuestionAny new version? PinsussAnonymous23-Mar-05 14:23 
AnswerRe: Any new version? PinmemberMX-Mauro29-Jun-05 13:00 
GeneralRe: Any new version? PinmemberdennisV17-Aug-05 21:56 
GeneralRe: Any new version? PinmemberMX-Mauro18-Aug-05 1:29 
GeneralRe: Any new version? PinsussAnonymous18-Aug-05 3:43 
GeneralRe: Any new version? PinmemberdennisV18-Aug-05 3:45 
GeneralRe: Any new version? PinmemberMX-Mauro18-Aug-05 7:40 
GeneralBackgound process PinmemberEmmanouel22-Jun-04 1:29 
GeneralRe: Backgound process PinmemberMX-Mauro22-Jun-04 4:15 
GeneralSingle-threaded application Pinmembermaciejr9-May-04 21:19 
GeneralRe: Single-threaded application PinmemberMX-Mauro10-May-04 2:05 
GeneralRe: Single-threaded application Pinmemberdis14119-Dec-06 21:57 
GeneralProgram crashed. PinmemberWREY16-Apr-04 11:59 
GeneralRe: Program crashed. PinmemberMX-Mauro16-Apr-04 12:03 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 20 Apr 2004
Article Copyright 2004 by Mauro Leggieri
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid