A Dialog class to drive a worker thread

This (very simple) dialog class, derived from CDialog, is meant to be used when you need to wake up a task at regular time intervals to do a specific job.  Why a dialog ?  Because it gives you some UI to specify parameters and also to start and stop the worker thread.  You'd use this class when you don't want to bother with NT services (or because your program has to run under Windows 95/98).  Why multi thread (you could just set a timer on the dialog) ? Because, by doing so the UI would be locked while the task is running (no redrawing, no ability to move the dialog, no response from controls).

To use this class, you simply create a dialog and a dialog class the usual way with Class Wizard, you modify it afterwards to derive from CMultiThreadDialog.  The button with IDOK should have the caption 'Start' and the button with IDCANCEL should have the caption 'Quit' or 'Close'.

When the dialog is started, it waits for the user to press the start button.  The derived dialog can allow the user to specify parameters that will be used by the thread when it will be running.  When the Start button is pressed, it modifies its caption to 'Stop', the Close button is disabled and the worker thread will have a chance to run at the first time the timer gets wake up (that means that you will not see anything happening for a certain laps of time, by default 10 s).

Of course, it's up to you to write the actual worker thread.  You do it by overriding the WorkerThread method.  For example, the worker thread you write could check if it has files to process (by looking for the presence of files in a specific directory), process them and quit.

 *	Returns TRUE if the Thread is currently running.
BOOL GetThreadIsRunning();
 *	Manages the start/stop feature of the dialog.  When you set
 *	running to TRUE, the UI modifies itself : the Start button becomes
 *	a Stop button, the Cancel (or Quit button) is disabled.
 *	When you set running to FALSE, the dialog reverses back to its
 *	initial state.
 *	When running is set to true, a timer is started and the Worker thread
 *	will be launched at the next time the timer is woke up.
 *	This method is called by the Start/Stop button.
void SetRunning(BOOL bRunning);
 *	Sets the timer interval at which the worker thread will be
 *	executed.
void SetTimerInterval(int nSeconds);
 *  Modifies the texts that appear on the Start/Stop button.
void SetStartStopText(LPCTSTR sStart, LPCTSTR sStop)
	m_sStart = sStart;
	m_sStop = sStop;
 *	This method has to be overriden in order to write
 *	code for the specific job that needs to be done.
 *	The default method just do nothing.
virtual UINT WorkerThread();
 *	This override is called whenever the state of the dialog
 *	is changed.  This is meant to disable/reenable the controls
 *	when the tread is started and stopped.
virtual void OnStateChanges(BOOL bNewState);

Download source - 3 KB

Date Last Updated: February 12, 1999


  • Memory Leak in the program

    Posted by Legacy on 03/03/1999 12:00am

    Originally posted by: Alan Tsai

    The author forget to DeleteCriticalSection()when CMultiThreadDlg is destructed and will cause resource leak.

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

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds