CreateTimerQueueTimer學習筆記 .

           定時器隊列(Timer Queue)可以使用CreateTimerQueue函數創建。定時器隊列中的定時器是輕量級對象,可以在一定時間間隔之後調用指定的回調函數(可以只調用一次,也可以是週期性的)。這種等待操作由線程池中某個線程處理的(系統級別)。

        向定時器隊列中添加定時器可以使用CreateTimerQueueTimer函數。更新一個計時器隊列中的計時器可以使用 ChangeTimerQueueTimer 函數。這兩個函數中你可以指定一個回調函數,如果設定時間到達,線程池中某個線程會調用該回調函數。

       使用 DeleteTimerQueueTimer函數可以取消掛起的計時器。當不再使計時器隊列的時候,調用 DeleteTimerQueueEx 函數刪除計時器隊列,該函數調用是會取消並刪除任何在該隊列中等待的計時器。

#include <iostream>
#include <windows.h> 
#include <stdio.h>
#include <tchar.h>

using namespace std;

//事件對象
HANDLE g_Event = NULL;
static  int COUNT = 0;

VOID CALLBACK TimerRoutine( PVOID lpParameter, BOOLEAN TimerOrWaitFired )
{
	if(NULL == lpParameter)
	{
		OutputDebugString(_T("lpParameter is null。。"));
		return;
	}

	printf("the values of param is :%d\n",*(int*)lpParameter);
	COUNT++;

	/*if(COUNT >= 3)
	{
		SetEvent(g_Event);
	}*/
}


int main(int argc,TCHAR *argv[])
{
	HANDLE hTimer= NULL;
	HANDLE hTimerQueue = NULL;
	int arg = 123;
	g_Event = CreateEvent(NULL,TRUE,FALSE,NULL);
	if(NULL == g_Event)
	{
		OutputDebugString(_T("CreateEvent失敗。。"));
		return 1;
	}

	hTimerQueue = CreateTimerQueue();
	if(NULL == hTimerQueue)
	{
		OutputDebugString(_T("CreateTimerQueue失敗。。"));
		return 1;
	}

	if(!CreateTimerQueueTimer(&hTimer,hTimerQueue,WAITORTIMERCALLBACK(TimerRoutine),&arg,5000,1000,NULL))
	{
		OutputDebugString(_T("CreateTimerQueueTimer失敗。。"));
		return 1;
	}

	if(WaitForSingleObject(g_Event,INFINITE) !=WAIT_OBJECT_0)
	{
		OutputDebugString(_T("WaitForSingleObject失敗。。"));
	}
   
	CloseHandle(g_Event);
	
	if(!DeleteTimerQueue(hTimerQueue))
	{
		OutputDebugString(_T("DeleteTimerQueue失敗。。"));
	}
	return 0;
}

發佈了41 篇原創文章 · 獲贊 5 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章