Click here to Skip to main content
15,895,142 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 307.7K   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

 
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
Jens Winslow18-Jul-03 12:47 
GeneralCorrection: AfxBeginThread(MyProcc, this-&gt;GetSafeHwnd()) NOT AfxBeginThread(MyProcc, this-&gt;AfxGetSafeHwnd()) Pin
Jens Winslow18-Jul-03 12:53
Jens Winslow18-Jul-03 12:53 
Generalusing new to declare member might work Pin
Jens Winslow21-Jul-03 5:40
Jens Winslow21-Jul-03 5:40 
GeneralRe: using new to declare member might work Pin
Anonymous22-Jul-03 19:24
Anonymous22-Jul-03 19:24 
GeneralRe: using new to declare member might work Pin
Jens Winslow23-Jul-03 3:59
Jens Winslow23-Jul-03 3:59 
GeneralRe: using new to declare member might work Pin
Enis23-Jul-03 4:17
Enis23-Jul-03 4:17 
GeneralRe: using new to declare member might work Pin
Jens Winslow23-Jul-03 7:09
Jens Winslow23-Jul-03 7:09 
GeneralWhy a Functor rather than structure with pointers you might ask Pin
Jens Winslow23-Jul-03 10:15
Jens Winslow23-Jul-03 10:15 
It occurred to me that people might wonder why I choose the more complicated use of a Functor instead of just using a structure with a this pointer and data parameters (now that I am done, I wonder too).

Let me share with you a tale of mystery and wonder - a tale of a painful journey! OMG | :OMG:

First I did try passing a pointer to a structure...

In my first structure based approach I defined a structure and enumerator to pass to a static UINT Command_T( LPVOID pCmdFunctor) which I could call from AfxBeginThread.
<br />
enum EChanCmd {<br />
		INITIALIZE,<br />
		SERVERCONNECT,<br />
		CLIENTCONNECT,<br />
		SEND,<br />
		RECEIVE,<br />
		CANCEL<br />
	};<br />
	<br />
	struct SChanCmd {<br />
		CMuxChannel* pChan;<br />
		SMuxChannelEventData* pParam;<br />
		EChanCmd eCmd;<br />
	}<br />


Command_T is a switch statement that then calls the appropriate command function in the CMuxChannel object pointed to by pChan. For debugging I used a TRACE before and after the call, calling pChan->GetStatus(), a member function created to tell me the status Hmmm | :| .

At first things seemed to work fineBig Grin | :-D . It was a bit of a pain to declare SChanCmd every time I wanted to issue a command with a worker thread, making for slightly complicated code like:

<br />
	SChanCmd uCmd;<br />
uCmd.pParam = new SMuxChannelEventData; //do not delete, call to MuxChan does this<br />
	uCmd.pParam->wp = (WPARAM)(USHORT)m_uiPortNum;<br />
	uCmd.pParam->lp = (LPARAM)(LPCTSTR)m_strIPAddr;<br />
uCmd.eCmd = CMuxChannel::CLIENTCONNECT; //command<br />
uCmd.pChan = &m_Chan;<br />
	AfxBeginThread(CMuxChannel::Command_T, &uCmd);<br />


Add to this a large ugly switch statement in Command_T, and my code was esteticaly not to pleasing. But after 3 days of battling with it I did not care! Dead | X|

Then I hit a minor snag.Cry | :((

Some of my CMuxChannel commands might throw an exception (which are appropriately handled in the class). To my surprise, if an exception occurs my uCmd->pChan is set to NULL!!!! God only knows what else may be happening! Confused | :confused: I am guessing that the unwinding of the heap / stack occurring during a throw/catch exception somehow blows away my pointer (I tried saving a backup and using it, but that did not work).Mad | :mad:
What ever is happening made me very nervous about the condition of m_Chan, and my ability to call functions from AfxBeginThread.Eek! | :eek:

I then tried using the Functor template (see link above), but I was not quite able to get it working, so I created my own less generic functor specifically for CMuxChannel. The concept was simple:
A class with a pointer to an instance of CMuxChannel, a function pointer to a command function in CMuxChannel, and a pointer to the parameter. Add a Call function to do the call, and a constructor taking a pointer to CMuxChannel and the desired function, and BINGO!

Now I can call my function like this, which looks a lot better:Big Grin | :-D
<br />
CMuxChannel::CCmdFunctor uCmd(&m_Chan, CMuxChannel::ConnectAsClient);<br />
	AfxBeginThread(CMuxChannel::Command_T, &uCmd);<br />


In reality the syntax for my Functor was murder to get right (had I known I would not have bothered, and just wrapped each command function in a static UINT AfxBeginThread function)Frown | :( But I did get it working at last.

<br />
//Functor class for use by static UNIT Command_T(LPVOID pParam) calling function,<br />
//allowing us to call functions in instance of CMuxChannel from AfxBeginThread<br />
class CCmdFunctor {<br />
public:<br />
	//constructor<br />
	CCmdFunctor(CMuxChannel* pChan, void (CMuxChannel::*ptrCmdFunc)(SMuxChannelEventData*), SMuxChannelEventData* pParam = NULL);<br />
<br />
	//call function to execute functor stored function<br />
	void Call() { (*m_pChan.*m_ptrCmdFunc)(m_pParam);}<br />
<br />
	CMuxChannel* m_pChan; //ptr to channel<br />
private:<br />
	//member vars<br />
	<br />
	void (CMuxChannel::*m_ptrCmdFunc)(SMuxChannelEventData*);<br />
	SMuxChannelEventData* m_pParam;<br />
};<br />
<br />
	static UINT Command_T( LPVOID pCmdFunctor); //function to command channel, callable by AfxBeginThread. Must point to functor<br />


In conclusion:
1) Functors, multi-threaded, and function pointers are not for the faint of heart! Eek! | :eek:
2) Something strange happens to your pointers to objects if the object uses / throws exceptions internally (feel free to explain this if you can) Confused | :confused:
3) Why did I ever want to become a programmer anyway? Unsure | :~

Perhaps this will help someone else (I hope) Cool | :cool:
Me, I am thinking about becoming a farmer Rose | [Rose] Rose | [Rose] Rose | [Rose]
Perhaps this was not a good day to code after all;P


Jens Winslow
GeneralAnd just one more thing....I hope Pin
Jens Winslow23-Jul-03 12:11
Jens Winslow23-Jul-03 12:11 
Generalthanks Pin
basil00320-Mar-03 13:28
basil00320-Mar-03 13:28 
GeneralYES!!!! Pin
aquawicket23-Oct-06 3:50
aquawicket23-Oct-06 3:50 
Generalthreads Pin
swirskiy28-Dec-02 15:38
swirskiy28-Dec-02 15:38 
Questionwhat about calls like UpdateData? Pin
Cadiolis5-Sep-02 11:18
Cadiolis5-Sep-02 11:18 
AnswerRe: what about calls like UpdateData? Pin
Enis5-Sep-02 21:08
Enis5-Sep-02 21:08 
GeneralRe: what about calls like UpdateData? Pin
Cadiolis6-Sep-02 6:20
Cadiolis6-Sep-02 6:20 
GeneralRe: what about calls like UpdateData? Pin
Jens Winslow21-Jul-03 6:04
Jens Winslow21-Jul-03 6:04 
AnswerRe: what about calls like UpdateData? Pin
Herben27-Sep-02 13:02
Herben27-Sep-02 13:02 
GeneralRe: what about calls like UpdateData? Pin
kfchow1-Feb-03 3:36
kfchow1-Feb-03 3:36 
GeneralRe: what about calls like UpdateData? Pin
JPD24-Nov-03 22:36
JPD24-Nov-03 22:36 
GeneralRe: what about calls like UpdateData? Pin
Gress19-Jun-04 10:23
Gress19-Jun-04 10:23 
GeneralRe: what about calls like UpdateData? Pin
David Crow15-Jul-04 3:34
David Crow15-Jul-04 3:34 
GeneralRe: what about calls like UpdateData? [modified] Pin
aquawicket23-Oct-06 4:31
aquawicket23-Oct-06 4:31 
GeneralRe: what about calls like UpdateData? Pin
David Crow23-Oct-06 4:50
David Crow23-Oct-06 4:50 
GeneralRe: what about calls like UpdateData? [modified] Pin
aquawicket24-Oct-06 13:14
aquawicket24-Oct-06 13:14 
GeneralTerminating Thread on Demand Pin
fergon20-Jun-02 5:01
professionalfergon20-Jun-02 5:01 

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.