//This is AsynchronousFunction class header file
typedef int (*functionCall)(int, int);
DWORD __stdcall functionExecuter(LPVOID pContext); // global function
class AsynchronousFunction
{
int param1, param2;
functionCall fCall;
HANDLE m_handle;
public:
AsynchronousFunction(functionCall, int, int);
~AsynchronousFunction();
int result();
protected:
private:
int returnVal;
};
It's implementation as follows
AsynchronousFunction::AsynchronousFunction(functionCall fCall, int param1, int param2):m_handle(CreateEvent(NULL , false , false , NULL))
{
bool b = QueueUserWorkItem(functionExecuter, this, WT_EXECUTEDEFAULT);
WaitForSingleObject(m_handle, INFINITE);
SetEvent(m_handle);
}
AsynchronousFunction::~AsynchronousFunction()
{
CloseHandle(m_handle);
}
int AsynchronousFunction::result()
{
return 0;// not implemented yet
}
DWORD __stdcall functionExecuter(LPVOID pContext)
{
return 0;
}
此处pContext接收“this”指针。我的尝试是从这里访问param1和param2
并执行工作 我该怎么做?如何将“this”指针传递给全局函数
我个人更喜欢避免朋友,除非绝对必要,甚至于是再次猜测,看看是否有其他方式。所以我更喜欢后一种方法。它看起来也很干净。 – jia103
@ jia103我同意你的意见。第二选项看起来更清洁我也亲自避免朋友功能 –
谢谢。我知道了。由于pContext是一个void指针,因此需要将其转换为AsynchronousFunction类型。 – Zarco