mfc多媒体定时器timeSetEvent

转:https://blog.csdn.net/tongsean/article/details/40562857

 

今天在调试项目的时候用到timeSetEvent,编译的时候没有出现错误,但运行的时候就会出现程序崩溃,在网上找了很多资料,参照MSDN,最终解决,特将遇到的问题和解决方法在此介绍,希望遇到同样问题的兄弟也能迅速定位问题。

查看MSDN,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;
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值