我尝试过用GetTickCount(),QueryPerformanceCounter()做定时器,然后读取系统时间。但是只有在定时为125ms的整数倍时,读出的时间间隔才是正确的;如果定时800ms,读出的时间间隔却不准确,如:UINT ShowTimeProc(LPVOID lParam)
{
CTDlg* pDlg=(CTDlg*)lParam;
DWORD dwstart,dwstop;
dwstop=GetTickCount();
while(1)
{
dwstart=dwstop;
::SendMessage(pDlg->m_hWnd,WM_UPDATETIME,0,0);
do
{
dwstop=GetTickCount();
}while(dwstop-125<dwstart);/////定时125ms
}
return 0;
}
结果如下:
2005 07 20 09 15 10.140
2005 07 20 09 15 10.265
2005 07 20 09 15 10.390
2005 07 20 09 15 10.515
2005 07 20 09 15 10.640
}while(dwstop-800<dwstart);/////定时800ms
结果如下:
2005 07 20 09 15 10.140
2005 07 20 09 15 10.952
2005 07 20 09 15 11.765
2005 07 20 09 15 12.577
2005 07 20 09 15 13.390
2005 07 20 09 15 14.140////////////中间间隔812或813ms,在最后的时候,总又能与第一次差5*800ms=4s,用QueryPerformanceCounter()也是同样,谁能给我解释一下,我希望能够定时100ms,结果如下:
2005 07 20 09 15 10.140
2005 07 20 09 15 10.240
2005 07 20 09 15 10.340
2005 07 20 09 15 10.440
该怎样实现?谢谢!请不吝赐教,高分酬谢,顶者有分。

解决方案 »

  1.   

    关注。
    ========
    Res
    The following table describes the resolution of the system timer. System Resolution 
    Windows NT 3.5 and later The system timer runs at approximately 10ms. 
    Windows NT 3.1 The system timer runs at approximately 16ms. 
    Windows 95 and later The system timer runs at approximately 55ms. 
      

  2.   

    你这样的定时器也不准确,并且太耗系统资源了。
    一GetTickCount返回值的精确度,二是线程可能被切换。可以考虑多媒体定时器Multimedia Timer Functions
    The following functions are used with multimedia timers.timeBeginPeriod
    timeEndPeriod
    timeGetDevCaps
    timeGetSystemTime
    timeGetTime
    timeKillEvent
    TimeProc
    timeSetEvent
      

  3.   

    可是我看到有人说可以精确到微秒级的,到毫秒应该不成问题吧。而且调试的时候我看了用QueryPerformanceCounter()方法是可以定时到100ms的,只是在这100ms间隔的时候取出系统时间,却不是100ms。怎么解释?
    to:  DentistryDoctor(My heart will fly,in the sky.) 
    多媒体定时器不会出现这样的问题吗?我怀疑并不是定时的精度有问题,而是系统时间的最小单位是1/2~n。