我想实现工作线程每隔一段时间(如20ms)执行一次如网络通信、或计算、或其他类型的工作,定时精度要求在ms级。该线程可采用高的优先级,且假设任务的执行时间不会超过定时器间隔。请问如何实现?能讲讲思路或具体的实现办法吗?谢谢!

解决方案 »

  1.   

    如果用一般的Windows定时器,好象精度不能满足你的要求。
    《VC++经典例程分析》里有个例子,能定时到1ms
      

  2.   

    太简单了
    SetTimer
    The SetTimer function creates a timer with the specified time-out value. UINT SetTimer(
      HWND hWnd,              // handle of window for timer messages
      UINT nIDEvent,          // timer identifier
      UINT uElapse,           // time-out value
      TIMERPROC lpTimerFunc   // address of timer procedure
    );
     
    Parameters
    hWnd 
    Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored. 
    nIDEvent 
    Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. 
    uElapse 
    Specifies the time-out value, in milliseconds. 
    lpTimerFunc 
    Pointer to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc. 
    If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message'sMSG structure contains the value of the hWnd parameter. Return Values
    If the function succeeds, the return value is an integer identifying the new timer. An application can pass this value, or the string identifier, if it exists, to the KillTimer function to destroy the timer. If the function fails to create a timer, the return value is zero. To get extended error information, call GetLastError.Res
    An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER. The wParam parameter of the WM_TIMER message contains the value of the nIDEvent parameter. QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Requires version 1.0 or later.
      Header: Declared in winuser.h.
      Import Library: Use user32.lib.See Also
    Timers Overview, Timer Functions, KillTimer,MSG, TimerProc, WM_TIMER  
      

  3.   

    settimer的精度不够,我查资料说timer的最小间隔为55ms左右。
      

  4.   


    QueryPerformanceCount可以精确地计算时间,但我不知道怎么用在这个线程里;难道在这个线程里用QueryPerformanceCount不停的查询时间,当到达时间间隔的时候,就执行相应的操作。但这样的CPU占用率会不会很高?
      

  5.   

    首先要问你对这个定时器需要执行的精度到底有多高?
    如果不大的话,可以另外开一个县城负责定时同志。
    如果精度不高,可以采用法消息的办法。
    如果精度比较高,我建议采用重载主县城的消息甭,然后同时等待消息和事件。
    定时县城设置事件,主县城等待事件,事件的优先级最好比消息高。这样就应该可以了。
    如果没事的话,倒也可以考虑在主线程中循环用QueryPerformanceCount:)hehe
      

  6.   

    to leeza
    thx
    能再说的详细点吗?你说另开一个线程负责定时,是怎么实现呀?是循环进行{sleep+发定时消息}吗?sleep的精度够吗?我想让定时器的误差低于1ms,不知能否实现?分不够的话,另开贴加分。谢谢~
      

  7.   

    QueryPerformanceCount可以精确地计算时间,
    那就算出到下次运行的时间间隔, Then  
    Sleep() ;
      

  8.   

    主要要有timeBeginPeriod(1),timeGetTime()
      

  9.   

    另一个县城计时用QueryPerformanceCount,然后循环判断即可。
    如果精度要求高,千万不要用sleep
    double span=0;
    LARGE_INTEGER b,e,freq;
    QueryPerformanceCount(&b);
    while(span<0.03)
    {
      QueryPerformanceCount(&e);
      span=double(e.quart-b.quart)/freq.quart;
    }
    大概就是这样子的。