1. 程式人生 > >Windows下的多媒體定時器:timeSetEvent 使用方法及易出錯的幾種情況

Windows下的多媒體定時器:timeSetEvent 使用方法及易出錯的幾種情況

MMRESULT timeSetEvent( UINT uDelay, UINT uResolution,  LPTIMECALLBACK lpTimeProc, WORD dwUser, UINT fuEvent )
       其中: uDelay:以毫秒指定事件的週期。
              Uresolution:以毫秒指定延時的精度,數值越小定時器事件解析度越高。預設值為1ms。
              LpTimeProc:指向一個回撥函式。
              DwUser:存放使用者提供的回撥資料。
              FuEvent:指定定時器事件型別:
              TIME_ONESHOT:uDelay毫秒後只產生一次事件
              TIME_PERIODIC :每隔uDelay毫秒週期性地產生事件。

參照函式的說明,函式很容易使用,但在使用的過程中應該注意以下幾點,嚴格按照MSDN的介紹:

1,回撥函式的使用

    在使用回撥函式的時候,一定要注意回撥函式的使用方法,MSDN上的說法如下,Pointer to a callback function that is called once upon expiration of a single event or periodically upon expiration of periodic events. If fuEventspecifies the TIME_CALLBACK_EVENT_SET or TIME_CALLBACK_EVENT_PULSE flag, then the lpTimeProc parameter is interpreted as a handle to an event object. The event will be set or pulsed upon completion of a single event or periodically upon completion of periodic events. For any other value of fuEvent, the lpTimeProc parameter is interpreted as a function pointer with the following signature: void (CALLBACK)(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);注意紅色部分在函式中一定不能丟掉,否則會引起程式的崩潰出現: 0x00000000 處有未經處理的異常: 0xC0000005: Access violation

2,timeKillEvent 關掉定時器的函式,一定要一一對應,每次timeSetEvent返回的定時器的ID是不一樣的,也就是說呼叫一次timeSetEvent就會產生一次Id,就是說,呼叫幾次timeSetEvent,就需要呼叫幾次timeKillEvent ,而且必須是相對應的ID,否則可能出現程式崩潰!!!!!

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

具體的程式程式碼除錯如下:

// timeset.cpp : 定義控制檯應用程式的入口點。
//
 
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
 
#pragma comment(lib,"Winmm.lib")
#define delaytime 1000
 
int  gtime_ID;
 
void  CALLBACK TimeEvent(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
	printf("time ID is %d, started,dwUser is %d\n",gtime_ID, dwUser);
	return;
}
 
void StartEventTime(DWORD_PTR duser)
{
	gtime_ID = timeSetEvent(delaytime,10,(LPTIMECALLBACK)TimeEvent,duser,TIME_PERIODIC);
	if(gtime_ID == NULL)
	{
		printf("time ID is not created\n");
		return;
	}
	return;
}
int _tmain(int argc, _TCHAR* argv[])
{
	int i = 0;
	while (1)
	{
		StartEventTime(i);  
		Sleep(1100);
		i++;
		timeKillEvent(gtime_ID); 
		
		if (i == 10)
		{
			break;
		}
	}
	return 0;
}
int  gtime_ID7,gtime_ID40,tmpcnt1=0,tmpcnt2=0;
#define delaytime 1
void  CALLBACK TimeEvent7(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{

	printf("%d time1:%d\n",tmpcnt1++,timeGetTime());
	return;
}

void  CALLBACK TimeEvent40(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{

	printf("                  %d time2:%d\n",tmpcnt2++,timeGetTime());
	return;
}


int main()
{

	gtime_ID7 = timeSetEvent(7,1,(LPTIMECALLBACK)TimeEvent7,0,TIME_PERIODIC);
	gtime_ID40 = timeSetEvent(40,1,(LPTIMECALLBACK)TimeEvent40,0,TIME_PERIODIC);
	Sleep(1000);

	timeKillEvent(gtime_ID7); 
	timeKillEvent(gtime_ID40); 

	getchar();
	return 0;
}