使用AfxBeginThread函数时出现问题. [英] Problem in using AfxBeginThread function.

查看:104
本文介绍了使用AfxBeginThread函数时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

我正在处理线程,并且在AfxBeginThread()&中遇到了问题.引发错误:错误C3867:"CThreadDlg :: WorkerThreadProc":函数调用缺少参数列表;使用& CThreadDlg :: WorkerThreadProc"创建指向成员的指针."

我的代码是这样的:

//ThreadDlg.h文件:

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:

class CThreadDlg:pblic CDialog
{
public:
UINT WorkerThreadProc( LPVOID Param );

};



在这里,AM不使用静态UINT WorkerThreadProc(LPVOID Param);
因为再次在WorkerThreadProc函数中,我将调用不是静态函数的其他函数.有一阵子,这里只是使用MessageBox函数.

//ThreadDlg.cpp文件:



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:

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);
}



有人可以帮我吗..如何正确使用AfxBeginThread ... !!

谢谢..



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

Thank you..

推荐答案

WorkerThreadProc必须是静态的,否则您不能将其作为函数的指针传递.静态方法与普通函数完全相同,但是成员函数可用于类的实例.如果能够传递它,则它将是方法的指针,并且还必须将实例传递给有问题的对象,因为这正是成员真正需要的.
最重要的是,仅由于AfxBeginThread的性质,就必须将其设置为静态.
如果需要将某些东西委派给实例,则必须将参数传递给带有实例的静态方法,然后将其强制转换回原始类.

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.

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

};





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);
}


您必须使用静态线程函数.要调用类的非静态成员,请使用pParam参数将this传递给AfxBeginThread.然后在静态线程函数中将pParam强制转换为指向您的类的指针.您还可以创建一个从静态线程函数调用的非静态函数:

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:

// [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);
}


这篇关于使用AfxBeginThread函数时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆