Click here to Skip to main content
14,027,634 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

GeneralWonderful! Thanks a lot Pin
claxcss1-Oct-14 21:37
memberclaxcss1-Oct-14 21:37 
GeneralMy vote of 1 Pin
Sunil P V28-Nov-12 20:27
memberSunil P V28-Nov-12 20:27 
GeneralMy vote of 1 Pin
Panic2k318-Sep-09 17:57
memberPanic2k318-Sep-09 17:57 
GeneralTHREADSTRUCT Not Needed Pin
Jonathan Wood4-Feb-09 4:33
memberJonathan Wood4-Feb-09 4:33 
GeneralDon't use this "trick" Pin
Roland_W5-Nov-08 11:01
memberRoland_W5-Nov-08 11:01 
GeneralGreat trick, thanks!! Pin
philbnt5-May-08 22:04
memberphilbnt5-May-08 22:04 
QuestionMulti Client Server Please Help! Pin
MauraV13-Oct-07 18:42
memberMauraV13-Oct-07 18:42 
QuestionUsing this method with a other class. Pin
CVianney9-Oct-07 22:25
memberCVianney9-Oct-07 22:25 
GeneralThank you very much Pin
liuxisheng_shizi5-Feb-07 16:31
memberliuxisheng_shizi5-Feb-07 16:31 
GeneralHelp for OpenGL Thread Pin
srinin00824-Jul-06 3:59
membersrinin00824-Jul-06 3:59 
GeneralAlso on memory leak Pin
jiac122-Apr-06 18:46
memberjiac122-Apr-06 18:46 
GeneralThanks Pin
dhammeriz12-Dec-05 7:09
memberdhammeriz12-Dec-05 7:09 
GeneralMemory leak on THREADSTRUCT Pin
srev21-Jun-05 2:54
membersrev21-Jun-05 2:54 
GeneralRe: Memory leak on THREADSTRUCT Pin
Anonymous19-Oct-05 2:45
memberAnonymous19-Oct-05 2:45 
GeneralRe: Memory leak on THREADSTRUCT Pin
ciyoung16-Feb-06 17:36
memberciyoung16-Feb-06 17:36 
GeneralUnhandled Exception Pin
Anonymous26-May-04 2:44
memberAnonymous26-May-04 2:44 
GeneralStop Thread Pin
aman20066-May-04 5:57
memberaman20066-May-04 5:57 
GeneralRe: Stop Thread Pin
Enis6-May-04 6:34
memberEnis6-May-04 6:34 
GeneralRe: Stop Thread Pin
j.v.r.25-Aug-10 7:47
memberj.v.r.25-Aug-10 7:47 
GeneralStop Thread Problem Pin
aman20065-May-04 14:52
memberaman20065-May-04 14:52 
MeterMan3-Mar-04 17:01
memberMeterMan3-Mar-04 17:01 
GeneralRe: HELP PLEASE Pin
Jens Winslow17-Mar-04 6:13
memberJens Winslow17-Mar-04 6:13 
GeneralRe: HELP PLEASE Pin
MeterMan19-Mar-04 11:02
memberMeterMan19-Mar-04 11:02 
GeneralRe: HELP PLEASE Pin
Jens Winslow22-Mar-04 6:34
memberJens Winslow22-Mar-04 6: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!) Pin
Jens Winslow18-Jul-03 12:47
memberJens Winslow18-Jul-03 12:47 

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
Web03 | 2.8.190419.4 | Last Updated 20 Jun 2002
Article Copyright 2002 by Enis
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid