步骤:
1、创建一个类似于void CALLBACK WaitCallBack( PTP_CALLBACK_INSTANCE pInstance, PVOID pvContext, PTP_WAIT pcbe, TP_WAIT_RESULT WaitResult)的函数
2、创建一个内核对象
3、CreateThreadpoolWait,创建等待
4、SetThreadpoolWait,设置等待
5、CloseThreadpoolWait、关闭内核对象
注意:
1、一次SetThreadpoolWait,一次调用回调函数
代码:
#include <iostream>
#include <afx.h>
using namespace std;
HANDLE g_hEvent;
void CALLBACK WaitCallBack(
PTP_CALLBACK_INSTANCE pInstance,
PVOID pvContext,
PTP_WAIT pcbe,
TP_WAIT_RESULT WaitResult)
{
cout << "线程ID:" << GetCurrentThreadId() << endl;
ResetEvent(g_hEvent);
}
void main()
{
g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
PTP_WAIT pWait = CreateThreadpoolWait(WaitCallBack, NULL, NULL);
SetThreadpoolWait(pWait, g_hEvent, NULL);
SetEvent(g_hEvent);
Sleep(200);
SetThreadpoolWait(pWait, g_hEvent, NULL);
SetEvent(g_hEvent);
Sleep(200);
WaitForThreadpoolWaitCallbacks(pWait, FALSE);
CloseThreadpoolWait(pWait);
CloseHandle(g_hEvent);
}
结果: