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

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

Most Popular Programming Stories

More for Developers

RSS Feeds

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