![]() |
General Programming »
Threads, Processes & IPC »
Threads
Intermediate
CThreadBy PEKCThread simplifies the process to start, run and stop threads. You can use it to work with threads in the same way as you do in C# or Java. |
C++/CLI, eVC, VC6, VC7, VC7.1, VC8.0, Win Mobile, .NET, Mobile, Win2K, WinXP, Win2003, Vista, TabletPC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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 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.
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().
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) { //Run the thread until someone has called Stop() while( ShouldRun() ) { ... } }
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.
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:
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.
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 :-).
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Aug 2005 Editor: Smitha Vijayan |
Copyright 2005 by PEK Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |