线程池之在内核对象触发时调用一个对象

步骤:

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

 结果:

  

posted @ 2013-09-26 13:19  Fly Hawk  阅读(333)  评论(0编辑  收藏  举报