Windows下的多媒体定时器:timeSetEvent 使用方法及易出错的几种情况

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mao0514/article/details/85600873

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

猜你喜欢

转载自blog.csdn.net/mao0514/article/details/85600873