Click here to Skip to main content
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

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)

Share

About the Author

Enis
Software Developer (Senior)
Romania Romania
No Biography provided

Comments and Discussions

 
GeneralWonderful! Thanks a lot Pinmemberclaxcss1-Oct-14 21:37 
GeneralMy vote of 1 Pinmembersunilkpv28-Nov-12 20:27 
GeneralMy vote of 1 PinmemberPanic2k318-Sep-09 17:57 
GeneralTHREADSTRUCT Not Needed PinmemberJonathan Wood4-Feb-09 4:33 
GeneralDon't use this "trick" PinmemberRoland_W5-Nov-08 11:01 
GeneralGreat trick, thanks!! Pinmemberphilbnt5-May-08 22:04 
QuestionMulti Client Server Please Help! PinmemberMauraV13-Oct-07 18:42 
QuestionUsing this method with a other class. PinmemberCVianney9-Oct-07 22:25 
GeneralThank you very much PinmemberMember #31823345-Feb-07 16:31 
GeneralHelp for OpenGL Thread Pinmembersrinin00824-Jul-06 3:59 
GeneralAlso on memory leak Pinmemberchiac2-Apr-06 18:46 
GeneralThanks Pinmemberdhammeriz12-Dec-05 7:09 
GeneralMemory leak on THREADSTRUCT Pinmembersrev21-Jun-05 2:54 
GeneralRe: Memory leak on THREADSTRUCT PinsussAnonymous19-Oct-05 2:45 
GeneralRe: Memory leak on THREADSTRUCT Pinmemberciyoung16-Feb-06 17:36 
GeneralUnhandled Exception PinsussAnonymous26-May-04 2:44 
GeneralStop Thread Pinmemberaman20066-May-04 5:57 
GeneralRe: Stop Thread PinmemberEnis6-May-04 6:34 
GeneralRe: Stop Thread Pinmemberj.v.r.25-Aug-10 7:47 
GeneralStop Thread Problem Pinmemberaman20065-May-04 14:52 
GeneralHELP PLEASE Pinmemberwin32newb3-Mar-04 17:01 
GeneralRe: HELP PLEASE PinmemberJens Winslow17-Mar-04 6:13 
I am just guessing here, based on my own challenges with multi-thread, but perhaps it will help you (?)
 
The multi thread functions must be static, and there are restrictions on the kinds of access static functons can have. I am guessing m_h0 is not static and therefore not directly acessible by the static multi thread func.
 
Basically a static function (and static variables) only exists once in memory, regardless of the number of times you instantiate an object from a class. This means a static function cannot know about the specific instance of an object and ita members (at least not directly.
 
I.e. if class CTest has member NotStatic, and static member Static, then
 
CTest T1;
CTest T2;
 
T1.NotStatic = 1;
T2.NotStatic = 2;
ASSERT( T1.NotStatic != T2.NotStatic ); //ok, no conflict
 
T1.Static = 1; //Static now 1
T2.Static = 2; //Static of ALL CTests now 2!!!
ASSERT( T1.Static != T2.Static ); //FAILS - CTest.Static all the same veriable.
 
A static function in CTest using NonStatic, would not directly know which instance (T1 or T2) to use, and therefore fails.
 
However, if you somehow pass the static function enough info to resolve the referencing (usually by a this pointer), you are ok.
 
Therefore you might try referencing m_h0 using the _this pointer - as far as I can see the THREADSTRUCT includes a _this pointer, which would let the static function access the specific instance of CCash3mfcDlg which holds the specific instance of m_h0. I am guessing the m_ball1 is also non static.
 

//m_ball1.SetIcon(m_h0); changed to use this pointer
ts->_this->m_ball1.SetIcon(ts->_this->m_h0);
 
Nitpicking: Why set the icon in a thread, rather than before calling thread in an initialize function? If you have to do it you should probably have a function
 
CCash3mfcDlg::SetIcon() {
m_ball1.SetIcon(m_h0);
}
 
and call it from your thread like this
ts->_this->SetIcon();
 
PS If you are just learning C++, you might want to start easier than multi threading OMG | :OMG: , but good luck, I admire your courage.
Smile | :)
GeneralRe: HELP PLEASE Pinmemberwin32newb19-Mar-04 11:02 
GeneralRe: HELP PLEASE Pinmemberjens 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!) PinmemberJens Winslow18-Jul-03 12:47 
GeneralCorrection: AfxBeginThread(MyProcc, this-&gt;GetSafeHwnd()) NOT AfxBeginThread(MyProcc, this-&gt;AfxGetSafeHwnd()) PinmemberJens Winslow18-Jul-03 12:53 
Generalusing new to declare member might work PinmemberJens Winslow21-Jul-03 5:40 
GeneralRe: using new to declare member might work PinsussAnonymous22-Jul-03 19:24 
GeneralRe: using new to declare member might work PinmemberJens Winslow23-Jul-03 3:59 
GeneralRe: using new to declare member might work PinmemberEnis23-Jul-03 4:17 
GeneralRe: using new to declare member might work PinmemberJens Winslow23-Jul-03 7:09 
GeneralWhy a Functor rather than structure with pointers you might ask PinmemberJens Winslow23-Jul-03 10:15 
GeneralAnd just one more thing....I hope PinmemberJens Winslow23-Jul-03 12:11 
Generalthanks Pinmemberbasil00320-Mar-03 13:28 
GeneralYES!!!! Pinmemberaquawicket23-Oct-06 3:50 
Generalthreads PinsussSwir28-Dec-02 15:38 
Questionwhat about calls like UpdateData? PinsussCadiolis5-Sep-02 11:18 
AnswerRe: what about calls like UpdateData? PinmemberEnis5-Sep-02 21:08 
GeneralRe: what about calls like UpdateData? PinsussCadiolis6-Sep-02 6:20 
GeneralRe: what about calls like UpdateData? PinmemberJens Winslow21-Jul-03 6:04 
AnswerRe: what about calls like UpdateData? PinsussHerben27-Sep-02 13:02 
GeneralRe: what about calls like UpdateData? Pinsusskfchow1-Feb-03 3:36 
GeneralRe: what about calls like UpdateData? PinmemberJPD24-Nov-03 22:36 
GeneralRe: what about calls like UpdateData? PinmemberGress19-Jun-04 10:23 
GeneralRe: what about calls like UpdateData? PinmemberDavidCrow15-Jul-04 3:34 
GeneralRe: what about calls like UpdateData? [modified] Pinmemberaquawicket23-Oct-06 4:31 
GeneralRe: what about calls like UpdateData? PinmemberDavidCrow23-Oct-06 4:50 
GeneralRe: what about calls like UpdateData? [modified] Pinmemberaquawicket24-Oct-06 13:14 
GeneralTerminating Thread on Demand Pinmemberfergon20-Jun-02 5:01 
GeneralRe: Terminating Thread on Demand PinmemberEnis20-Jun-02 5:14 

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 | Mobile
Web03 | 2.8.141015.1 | Last Updated 20 Jun 2002
Article Copyright 2002 by Enis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid