Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / MFC
Article

Using AfxBeginThread with class member controlling function

Rate me:
Please Sign up or sign in to vote.
3.73/5 (43 votes)
19 Jun 2002CPOL 306.1K   3.6K   29   68
Create worker threads with class member controlling function

Introduction

Creating a separate thread in your application in order to execute some time consuming operations is very simple. You just call AfxBeginThread() with the appropriate parameters and that's it. But Microsoft wants the controlling function either to be a global function or to be a static class member function; which in both cases means that you don't have access to your class member variables or methods from within the controlling function. This article shows you a little trick on how to do this and keep your source OO.

Step-by-Step

  1. Create your project as usual, add your dialogs, controls and all the stuff.
  2. Create your classes for the dialogs using Class Wizard, and assign variables to your controls.
  3. For the class you want to implement multithreading, edit the header file adding the following code:
    //controlling function header
    static UINT StartThread (LPVOID param);
    
    //structure for passing to the controlling function
    typedef struct THREADSTRUCT
    {
        CThreadDemoDlg*    _this;
            //you can add here other parameters you might be interested on
    } THREADSTRUCT;
  4. In the implementation file for your class, add the following code:
    UINT CThreadDemoDlg::StartThread (LPVOID param)
    {
        THREADSTRUCT*    ts = (THREADSTRUCT*)param;
    
        //here is the time-consuming process 
        //which interacts with your dialog
        AfxMessageBox ("Thread is started!");
    
            //see the access mode to your dialog controls
        ts->_this->m_ctrl_progress.SetRange (0, 1000);  
        while (ts->_this->m_ctrl_progress.GetPos () < 1000)
        {
            Sleep(500);
            ts->_this->m_ctrl_progress.StepIt ();
        }
    
        //you can also call AfxEndThread() here
        return 1;
    }
    void CThreadDemoDlg::OnStart() 
    {
            //call the thread on a button action or menu
        THREADSTRUCT *_param = new THREADSTRUCT;
        _param->_this = this;
        AfxBeginThread (StartThread, _param);
    }
  5. Now you can test your program!

Conclusion

I hope this will be helpful for you. I've been using CodeProject for a long time and this article is the first step for payback.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Terminating Thread on Demand Pin
fergon20-Jun-02 5:32
professionalfergon20-Jun-02 5:32 
GeneralRe: Terminating Thread on Demand Pin
Enis20-Jun-02 5:58
Enis20-Jun-02 5:58 
GeneralRe: Terminating Thread on Demand Pin
fergon20-Jun-02 6:07
professionalfergon20-Jun-02 6:07 
GeneralBetter solution Pin
19-Jun-02 13:47
suss19-Jun-02 13:47 
GeneralMemory leak Pin
Nish Nishant19-Jun-02 10:39
sitebuilderNish Nishant19-Jun-02 10:39 
GeneralRe: Memory leak Pin
Enis19-Jun-02 11:34
Enis19-Jun-02 11:34 
GeneralRe: Memory leak Pin
Nish Nishant19-Jun-02 11:40
sitebuilderNish Nishant19-Jun-02 11:40 
GeneralRe: Memory leak Pin
Enis19-Jun-02 12:04
Enis19-Jun-02 12:04 
GeneralRe: Memory leak Pin
Nish Nishant19-Jun-02 12:10
sitebuilderNish Nishant19-Jun-02 12:10 
GeneralRe: Memory leak Pin
19-Jun-02 12:31
suss19-Jun-02 12:31 
GeneralRe: Memory leak Pin
Nish Nishant19-Jun-02 12:37
sitebuilderNish Nishant19-Jun-02 12:37 
GeneralThread Safety Pin
DangerTenor19-Jun-02 9:23
DangerTenor19-Jun-02 9:23 
QuestionWhy? Pin
19-Jun-02 4:36
suss19-Jun-02 4:36 
AnswerRe: Why? Pin
Jim A. Johnson19-Jun-02 4:44
Jim A. Johnson19-Jun-02 4:44 
AnswerDon't forget Pin
19-Jun-02 5:32
suss19-Jun-02 5:32 
GeneralRe: Don't forget Pin
Enis19-Jun-02 6:26
Enis19-Jun-02 6:26 
GeneralRe: Don't forget Pin
19-Jun-02 7:50
suss19-Jun-02 7:50 
GeneralRe: Don't forget Pin
Anonymous28-Jan-03 6:33
Anonymous28-Jan-03 6:33 

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.