Click here to Skip to main content
13,768,132 members
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 18 Jun 2002
Licenced CPOL

Using AfxBeginThread with class member controlling function

, 19 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Create worker threads with class member controlling function


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.


  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
  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)
            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!


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.


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


About the Author

Software Developer (Senior)
Romania Romania
No Biography provided

You may also be interested in...


Comments and Discussions

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.181116.1 | Last Updated 20 Jun 2002
Article Copyright 2002 by Enis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid