Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi Everyone..

Am working on Threads, And am facing problem in AfxBeginThread() & throws error: "error C3867: 'CThreadDlg::WorkerThreadProc': function call missing argument list; use '&CThreadDlg::WorkerThreadProc' to create a pointer to member".

My code is like this:

//ThreadDlg.h file:
C++
class CThreadDlg:pblic CDialog
{
public:
UINT WorkerThreadProc( LPVOID Param );

};


Here, AM not using static UINT WorkerThreadProc( LPVOID Param );
because again in WorkerThreadProc function, i will be calling other functions which are not static functions. For a while, Here am just using MessageBox function.

//ThreadDlg.cpp file:
C++
UINT CThreadDlg::WorkerThreadProc(LPVOID Param)
{
	MessageBox("Am in Thread Function","",MB_OK);

	return 0;
}


void CThreadDlg::OnBnClickedOk()
{
AfxBeginThread( WorkerThreadProc,NULL,THREAD_PRIORITY_NORMAL,0,0,NULL); //Error C3867.

MessageBox("Thread started","",MB_OK);
}


Can Anyone help me.. How to properly use AfxBeginThread...!!

Thank you..
Posted
Comments
[no name] 28-Aug-12 7:02am    
Did you try AfxBeginThread( &WorkerThreadProc,NULL,THREAD_PRIORITY_NORMAL,0,0,NULL);
Guru_C++ 28-Aug-12 7:18am    
yes.. I tried it.. But still error : "error C2276: '&' : illegal operation on bound member function expression".

WorkerThreadProc has to be static otherwise you cannot pass it as a pointer to a function. A static method is exactly the same as a normal function but a member function works on an instance of a class. If you were able to pass it, it would be a pointer to a method and you would also have to pass the instance to the object in question because that is what a member really needs.
Bottom line, you have to make it static just because of the nature of AfxBeginThread.
If you need to delegate something to the instance you have to pass a parameter to the static method with the instance and then cast it back to the original class.

C#
class CThreadDlg:public CDialog
{
protected:
  UINT WorkerThreadProc();
public:
  static UINT WorkerThreadProcStatic( LPVOID Param );

};


C#
UINT CThreadDlg::WorkerThreadProc(){
    MessageBox("Am in Thread Function and have instance","",MB_OK);

    return 0;

}

UINT CThreadDlg::WorkerThreadProcStatic(LPVOID Param)
{
    CThreadDlg* dlg = (CThreadDlg*)Param;
    dlg->WorkerThreadProc();
}


void CThreadDlg::OnBnClickedOk()
{
AfxBeginThread( WorkerThreadProcStatic,this,THREAD_PRIORITY_NORMAL,0,0,NULL); 

MessageBox("Thread started","",MB_OK);
}
 
Share this answer
 
v2
Comments
Guru_C++ 29-Aug-12 1:07am    
Thank you.. This is also fine. Now am having clear idea..
You must use a static thread function. To call non static members of your class, pass this to AfxBeginThread using the pParam parameter. Then cast the pParam in your static thread function to be a pointer to your class. You may also create a non static function that is called from the static thread function:

C++
// [EDIT]
// In the header file:
//static UINT WorkerThread(LPVOID pParam);
//UINT WorkerThreadProc();
// [/EDIT]

// Static thread function
/*static*/ UINT CThreadDlg::WorkerThread(LPVOID pParam)
{
    ASSERT(pParam != NULL);
    CThreadDlg* pThis = reinterpret_cast<CThreadDlg*>(pParam);
    // Call non static functions here using pThis,
    //  or just call the non static version of your thread function.
    return pThis->WorkerThreadProc();
}

// Non static thread function.
UINT CThreadDlg::WorkerThreadProc(void)
{
    MessageBox("In non static thread function","",MB_OK);
    return 0;
}

void CThreadDlg::OnBnClickedOk()
{
    AfxBeginThread(WorkerThread,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
    MessageBox("Thread started","",MB_OK);
}
 
Share this answer
 
v2
Comments
Philip Stuyck 28-Aug-12 9:36am    
your header file is incorrect. The poster is going to be totally confused.
Jochen Arndt 28-Aug-12 9:46am    
I just did not show a complete header file, but only the relevant lines. I don't think that the poster is confused by it. However, I will edit my answer by commenting it out.
Guru_C++ 29-Aug-12 1:05am    
Thanks Jochen.. It is perfect .. ANd.. I dint confused.. It is working fine..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900