我使用了两个timeSetEvent函数,同时循环执行两件事情。使用回调函数进行触发循环执行。
结果:两个timeSetEvent函数的返回值均不为零(下文程序中的ID1=16,ID2=33),说明定时器已经创建成功了,但是2个回调函数只有第一个回调函数执行(先创建的那个定时器的回调函数能执行,后创建的定时器的回调函数不能运行)。即下文中的TimeCallBack1函数能执行,TimeCallBack2函数中的代码却不执行。如果把其中一个定时器注释掉,另外一个程序运行时正常的。一直找不到原因,急啊。各位高手能否帮我分析分析,谢谢!程序很大,下面是程序的框架:
//a.cpp
ID1 = timeSetEvent(1,1,(LPTIMECALLBACK)TimeCallBack1, (DWORD)this,TIME_PERIODIC);
void PASCAL TimeCallBack1(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
//
}
//b.cpp
ID2 = timeSetEvent(10,1,(LPTIMECALLBACK)TimeCallBack2, (DWORD)this,TIME_PERIODIC);
void PASCAL TimeCallBack2(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
//

解决方案 »

  1.   

    初步怀疑是执行了你没发觉!你没有断一下试试么?你为什么要开两个定时器呢,按你这种需求的话在第一个定时器执行10次的时候执行第二个定时器的内容不就可以了么!只不过增加一个static int而已!
      

  2.   

    我设置断点了调试了,确实没执行。主要是第二个线程执行周期大于1ms,所以不能和第一个线程放在一起执行。否则第一个线程达不到1ms了。
      

  3.   

    1ms,对于windows来说,这个有点难。你设置的断点如何,是不是非断点行,不起左右的调试行。
    建议你:timeSetEvent函数已经过时,它是原来微软提供的一个多媒体组件库,Microsoft 建议你用CreateTimerQueueTimer。
      

  4.   

    MMRESULT timeSetEvent(
      UINT           uDelay,      
      UINT           uResolution, 
      LPTIMECALLBACK lpTimeProc,  
      DWORD_PTR      dwUser,      
      UINT           fuEvent      
    );
    ParametersuDelay:Event delay, in milliseconds. If this value is not in the range of the minimum and maximum event delays supported by the timer, the function returns an error.uResolution:Resolution of the timer event, in milliseconds. The resolution increases with smaller values; a resolution of 0 indicates periodic events should occur with the greatest possible accuracy. To reduce system overhead, however, you should use the maximum value appropriate for your application.判断一下调用timeSetEvent的返回值吧,看看是否成功。
      

  5.   

    MMRESULT timeSetEvent(
      UINT           uDelay,      
      UINT           uResolution, 
      LPTIMECALLBACK lpTimeProc,  
      DWORD_PTR      dwUser,      
      UINT           fuEvent      
    );
    fuEvent 
    Timer event type. This parameter may include one of the following values. Value Meaning 
    TIME_ONESHOT Event occurs once, after uDelay milliseconds. 
    TIME_PERIODIC Event occurs every uDelay milliseconds. TIME_PERIODIC周期到了会不会把计数器清了,
    第二个函数根本没有触发条件
      

  6.   

    不同意,照你意思多个定时器只要周期不同的话只会执行周期短的了?自己去测试下吧。楼主的1ms的周期对timesetevent来说太快了,CPU占用率应该很高,从而导致第二个定时器来不及执行,可以试着将第一个定时器的周期换成5ms以上试试,用timesetevent来做多个定时器肯定是可以的