Introduction
CThread
makes it possible to work with threads in MFC pretty much the same way as you do in C#/Java. You derive your own class from CThread
and implement Run(void* )
. To start the thread, you have to call Start()
. Now a new thread starts it's execution in Run()
. When Run()
is finished, the thread stops its execution.
Using the code
To use the code you should derive your class from CThread
. You must also implement the Run
function, where the thread executes. Example:
class CMyThread : public CThread
{
void Run(void* param);
}
It's OK if you want to derive from several classes, which is common if you want to run the thread in a dialog class:
class CMyThread : public CDialog, CThread
{
void Run(void* param);
}
Starting the thread
Starting the thread is very simple, just call Start()
. Now a new thread starts its execution in Run()
. You should not call Start()
if a thread is already running. To check this, call IsRunning()
.
You could send an optional parameter to the thread with a pointer. If you do this, make sure that the pointer is valid. The following example will not work:
{
int parameter = 10;
Start(¶mater);
}
When you call Start()
the pointer is valid, but it may not always be so when the thread starts to execute. Use new
instead:
{
int* parameter = new int(10);
Start(¶mater);
}
Don't forget to delete the object in Run()
. The parameter is sent to the thread in Run()
as a void
pointer. You could also use member variables to send information to the thread.
Stopping the thread
To stop the thread, call Stop()
. This will not stop the thread immediately; instead it sets a flag which tells the thread to stop its execution. If you want to wait for the thread to exit, call WaitForStop()
.
Running the thread
What the thread has to do is up to you. The thread will start its execution in Run()
, the function that you must implement. When the thread exits Run()
, it will finish its execution. When you implement Run()
, you must check if the thread should stop its execution. This is true if some thread has called Stop()
. To check the flag, call ShouldRun()
. Example:
void CMyThread::Run(void* param)
{
while( ShouldRun() )
{
...
}
}
Sending messages from a thread
Very often you want to update the GUI from the thread. For example, you want to update a progress bar. The obvious solution is to update the progress bar directly from the thread. Example:
void CMyThreadInDialog::Run(void* param)
{
...
m_progressbar.SetPos(10)
...
}
But this will not work. A golden rule in Windows is that only one thread should work with the GUI. Instead of this you should use messages. From the thread that is executing in Run()
you should send a message to the main thread which tells the window to make the update. You should not use SendMessage
, use PostMessage
instead. SendMessage
updates the GUI directly from the thread, which is not what you want. PostMessage
adds a message to the window's messages queue and will be handled from the main thread.
So your code should look something like this:
void CMyThreadInDialog::Run(void* param)
{
...
::PostMessage(UPDATE_PROGRESSBAR, m_hWnd, 10, 0);
...
}
See the example for more details.
Points of interest
Before I wrote this class, I looked for something similar in CodeProject. To my surprise, I didn't find any article on this topic. But I wasn't looking hard enough... There are at last two articles that provide a similar solution:
- CThread
A more complex solution than my CThread
(by the way, I'm sorry about the name conflict). But it's interesting to look at it.
- AG_Thread
This is a more simple solution than my CThread
.
Even if I am not the first one to write an article on this topic, I hope someone finds this class useful :-).
History
- 30th July, 2005 - Version 1.0. Initial version.
- 21st August, 2005 - Version 1.01. Minor fixes and improvements suggested by Blake Miller.