Click here to Skip to main content
11,412,159 members (62,068 online)
Click here to Skip to main content

Using AfxBeginThread with class member controlling function

, 19 Jun 2002 CPOL
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

Comments and Discussions

GeneralWonderful! Thanks a lot Pinmemberclaxcss1-Oct-14 22:37 
GeneralMy vote of 1 Pinmembersunilkpv28-Nov-12 21:27 
GeneralMy vote of 1 PinmemberPanic2k318-Sep-09 18:57 
GeneralTHREADSTRUCT Not Needed PinmemberJonathan Wood4-Feb-09 5:33 
GeneralDon't use this "trick" PinmemberRoland_W5-Nov-08 12:01 
GeneralGreat trick, thanks!! Pinmemberphilbnt5-May-08 23:04 
QuestionMulti Client Server Please Help! PinmemberMauraV13-Oct-07 19:42 
QuestionUsing this method with a other class. PinmemberCVianney9-Oct-07 23:25 
GeneralThank you very much PinmemberMember #31823345-Feb-07 17:31 
GeneralHelp for OpenGL Thread Pinmembersrinin00824-Jul-06 4:59 
GeneralAlso on memory leak Pinmemberchiac2-Apr-06 19:46 
GeneralThanks Pinmemberdhammeriz12-Dec-05 8:09 
GeneralMemory leak on THREADSTRUCT Pinmembersrev21-Jun-05 3:54 
GeneralRe: Memory leak on THREADSTRUCT PinsussAnonymous19-Oct-05 3:45 
GeneralRe: Memory leak on THREADSTRUCT Pinmemberciyoung16-Feb-06 18:36 
GeneralUnhandled Exception PinsussAnonymous26-May-04 3:44 
GeneralStop Thread Pinmemberaman20066-May-04 6:57 
GeneralRe: Stop Thread PinmemberEnis6-May-04 7:34 
GeneralRe: Stop Thread Pinmemberj.v.r.25-Aug-10 8:47 
GeneralStop Thread Problem Pinmemberaman20065-May-04 15:52 
GeneralHELP PLEASE Pinmemberwin32newb3-Mar-04 18:01 
Cry | :(( I used this example and it works fine if i do it exactly like he has. But what if i actually want to do something inside the thread. I can't.
I have member control called m_ball1 and a HICON called m_h0;
I want to update the m_ball1 within the thread but i can't.
i keep getting the error
error C2228: left of '.SetIcon' must have class/struct/union type
error C2597: illegal reference to data member 'CCash3mfcDlg::m_h0' in a static member function

I have everything exactly like the author of this demo has except my Classname is different I used CCash3mfcDlg.

I have this in the cpp. If i // it out the program works just like the demo.
What am I doing wrong cause i would really like to incorporate this into my program.

UINT CCash3mfcDlg::StartThread(LPVOID param)
     THREADSTRUCT*     ts = (THREADSTRUCT*)param;
     //here is the time-consuming process which interacts with your dialog
     AfxMessageBox ("Thread is started!");
     ts->_this->m_processstatus.SetRange (0, 1000);
     while (ts->_this->m_processstatus.GetPos () < 10000000)


          ts->_this->m_processstatus.StepIt ();

     //you can also call AfxEndThread() here
     return 1;

Just starting to get into Visual C++ so give me some slack please.
GeneralRe: HELP PLEASE PinmemberJens Winslow17-Mar-04 7:13 
GeneralRe: HELP PLEASE Pinmemberwin32newb19-Mar-04 12:02 
GeneralRe: HELP PLEASE Pinmemberjens winslow22-Mar-04 7:34 
GeneralI thought you were supposed to pass handles, not pointers to objects into threads??? Not that that seems to work for me (HELP Please!) PinmemberJens Winslow18-Jul-03 13:47 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 20 Jun 2002
Article Copyright 2002 by Enis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid