Windows下有一个多媒体定时器,用法为一组API函数的调用,它们是:
MMRESULT timeBeginPeriod( UINT uPeriod ) ;
MMRESULT timeSetEvent( UINT uDelay,
UINT uResolution,
LPTIMECALLBACK lpTimeProc,
DWORD dwUser,
UINT fuEvent
);
void CALLBACK TimeProc( UINT uID,
UINT uMsg,
DWORD dwUser,
DWORD dw1,
DWORD dw2
);
MMRESULT timeKillEvent( UINT uTimerID );
MMRESULT timeEndPeriod( UINT uPeriod );
其中timeBeginPeriod是用来设置最高定时精度的,最高精度为1ms,如果要产生间隔为1ms的中断,必须调用timeBeginPeriod(1);当定时器用完之后就要用timeEndPeriod(1);来恢复默认的精度。具体使用方法为在timeBeginPeriod(1)调用之后用timeSetEvent()注册一个回调函数,即一个中断处理过程。它还可以向回调函数传递一个参数,通常可以传送一个窗口句柄之类的东西。而回调函数TimeProc则从dwwUser参数中取出传递的参数使用。在Windows下,可以用这种方法进行1ms精度的定时数据采集,数据发送,但要保证1ms能完成所有的操作和运算。