Click here to Skip to main content
Licence 
First Posted 15 Apr 2004
Views 82,572
Bookmarked 23 times

Background Task Dialog

By | 19 Apr 2004 | Article
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 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

About the Author

Mauro Leggieri

Web Developer

Argentina Argentina

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCan we achieve this without a &quot;Dialog&quot; [modified] PinmemberIScribe15:25 3 Jun '07  
GeneralSome remarks Pinmembercristitomi5:54 22 Mar '07  
GeneralRe: Some remarks PinmemberMauro Leggieri9:51 22 Mar '07  
GeneralRe: Some remarks Pinmembercristitomi12:38 22 Mar '07  
GeneralRe: Some remarks PinmemberMauro Leggieri3:35 23 Mar '07  
GeneralRe: Some remarks PinmemberMember 36910:34 15 Mar '09  
GeneralRe: Some remarks PinmemberMauro Leggieri10:23 15 Mar '09  
GeneralRe: Some remarks [modified] PinmemberMember 369115:25 15 Mar '09  
GeneralRe: Some remarks PinmemberMauro Leggieri7:15 16 Mar '09  
Questionwhat to do when using multiple base class? Pinmember_kane_8:21 27 Dec '05  
AnswerRe: what to do when using multiple base class? PinmemberMauro Leggieri8:50 27 Dec '05  
GeneralRe: what to do when using multiple base class? Pinmember_kane_7:04 29 Dec '05  
GeneralRe: what to do when using multiple base class? PinmemberMauro Leggieri8:49 29 Dec '05  
GeneralRC -4 after 85 DoModal()s PinsussAnonymous3:40 29 Jun '05  
GeneralRe: RC -4 after 85 DoModal()s PinmemberMX-Mauro12:36 29 Jun '05  
QuestionAny new version? PinsussAnonymous14:23 23 Mar '05  
AnswerRe: Any new version? PinmemberMX-Mauro13:00 29 Jun '05  
GeneralRe: Any new version? PinmemberdennisV21:56 17 Aug '05  
GeneralRe: Any new version? PinmemberMX-Mauro1:29 18 Aug '05  
GeneralRe: Any new version? PinsussAnonymous3:43 18 Aug '05  
GeneralRe: Any new version? PinmemberdennisV3:45 18 Aug '05  
GeneralRe: Any new version? PinmemberMX-Mauro7:40 18 Aug '05  
GeneralBackgound process PinmemberEmmanouel1:29 22 Jun '04  
GeneralRe: Backgound process PinmemberMX-Mauro4:15 22 Jun '04  
GeneralSingle-threaded application Pinmembermaciejr21:19 9 May '04  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 20 Apr 2004
Article Copyright 2004 by Mauro Leggieri
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid