Click here to Skip to main content
15,886,689 members
Articles / Mobile Apps
Article

CThread

Rate me:
Please Sign up or sign in to vote.
1.60/5 (6 votes)
23 Aug 2005Ms-RL3 min read 67.3K   1.6K   26   13
CThread 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.

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(&paramater);
}

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(&paramater);
}

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)
{
    //Run the thread until someone has called Stop()
    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.

License

This article, along with any associated source code and files, is licensed under Microsoft Reciprocal License


Written By
PEK
Sweden Sweden
PEK is one of the millions of programmers that sometimes program so hard that he forgets how to sleep (this is especially true when he has more important things to do). He thinks that there are not enough donuts in the world. He likes when his programs works as they should do, but dislikes when his programs is more clever than he is.

Comments and Discussions

 
Questionporting to vs 2010 Pin
Member 121461378-Jan-16 11:44
Member 121461378-Jan-16 11:44 
GeneralCThread and ISAPI Pin
luca98130-Aug-05 0:36
luca98130-Aug-05 0:36 
GeneralRe: CThread and ISAPI Pin
PEK30-Aug-05 8:36
PEK30-Aug-05 8:36 
GeneralMore comments... Pin
Rolf Schaeuble23-Aug-05 3:49
Rolf Schaeuble23-Aug-05 3:49 
GeneralRe: More comments... Pin
PEK30-Aug-05 8:46
PEK30-Aug-05 8:46 
GeneralComments and Suggestions Pin
Blake Miller1-Aug-05 10:50
Blake Miller1-Aug-05 10:50 
GeneralRe: Comments and Suggestions Pin
PEK2-Aug-05 9:56
PEK2-Aug-05 9:56 
GeneralRe: Comments and Suggestions Pin
Blake Miller2-Aug-05 10:17
Blake Miller2-Aug-05 10:17 
GeneralRe: Comments and Suggestions Pin
PEK4-Aug-05 9:33
PEK4-Aug-05 9:33 
GeneralRe: Comments and Suggestions Pin
Blake Miller4-Aug-05 11:00
Blake Miller4-Aug-05 11:00 
GeneralRe: Comments and Suggestions Pin
PEK5-Aug-05 10:40
PEK5-Aug-05 10:40 
GeneralRe: Comments and Suggestions Pin
Blake Miller5-Aug-05 10:51
Blake Miller5-Aug-05 10:51 
GeneralRe: Comments and Suggestions Pin
PEK6-Aug-05 1:10
PEK6-Aug-05 1:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.