Click here to Skip to main content
15,878,959 members
Articles / Desktop Programming / MFC
Article

Background Task Dialog

Rate me:
Please Sign up or sign in to vote.
3.55/5 (6 votes)
19 Apr 2004CPOL1 min read 122.1K   2.3K   21   29
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

Introduction

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:

public:
  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)
  {
     AfxMessageBox(_T("Error"));
     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.

License

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


Written By
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 http://www.mauroleggieri.com.ar

Comments and Discussions

 
Questionwhat to do when using multiple base class? Pin
_kane_27-Dec-05 8:21
_kane_27-Dec-05 8:21 
AnswerRe: what to do when using multiple base class? Pin
Mauro Leggieri27-Dec-05 8:50
Mauro Leggieri27-Dec-05 8:50 
GeneralRe: what to do when using multiple base class? Pin
_kane_29-Dec-05 7:04
_kane_29-Dec-05 7:04 
GeneralRe: what to do when using multiple base class? Pin
Mauro Leggieri29-Dec-05 8:49
Mauro Leggieri29-Dec-05 8:49 

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.