线程池之每隔一段时间调用一个函数
步骤:
1、创建一个类似于void CALLBACK TimeoutCallBack(PTP_CALLBACK_INSTANCE pInstance, PVOID pvContext, PTP_TIMER pcbe)的函数
2、CreateThreadpoolTimer
3、SetThreadpoolTimer
4、CloseThreadpoolTimer
理解:
其实就是相当于一个可等待的内核对象,不可在实现上,采用了线程池的技术。
代码:
#include <iostream> #include <afx.h> using namespace std; void CALLBACK TimeoutCallBack( PTP_CALLBACK_INSTANCE pInstance, PVOID pvContext, PTP_TIMER pcbe) { cout << "线程ID:" << GetCurrentThreadId() << endl; } void main() { PTP_TIMER pTimer = CreateThreadpoolTimer(TimeoutCallBack, NULL, NULL); SYSTEMTIME st; FILETIME ftLocal, ftUTC; LARGE_INTEGER liUTC; st.wYear = 2013; st.wMonth = 9; st.wDay = 26; st.wDayOfWeek = 0; st.wHour = 12; st.wMinute = 46; st.wSecond = 0; st.wMilliseconds = 0; SystemTimeToFileTime(&st, &ftLocal); LocalFileTimeToFileTime(&ftLocal, &ftUTC); liUTC.LowPart = ftUTC.dwLowDateTime; liUTC.HighPart = ftUTC.dwHighDateTime; SetThreadpoolTimer(pTimer, &ftUTC, 1000, 0); Sleep(2000); WaitForThreadpoolTimerCallbacks(pTimer, TRUE); CloseThreadpoolTimer(pTimer); }
结果: