太简单了 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
《VC++经典例程分析》里有个例子,能定时到1ms
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
QueryPerformanceCount可以精确地计算时间,但我不知道怎么用在这个线程里;难道在这个线程里用QueryPerformanceCount不停的查询时间,当到达时间间隔的时候,就执行相应的操作。但这样的CPU占用率会不会很高?
如果不大的话,可以另外开一个县城负责定时同志。
如果精度不高,可以采用法消息的办法。
如果精度比较高,我建议采用重载主县城的消息甭,然后同时等待消息和事件。
定时县城设置事件,主县城等待事件,事件的优先级最好比消息高。这样就应该可以了。
如果没事的话,倒也可以考虑在主线程中循环用QueryPerformanceCount:)hehe
thx
能再说的详细点吗?你说另开一个线程负责定时,是怎么实现呀?是循环进行{sleep+发定时消息}吗?sleep的精度够吗?我想让定时器的误差低于1ms,不知能否实现?分不够的话,另开贴加分。谢谢~
那就算出到下次运行的时间间隔, Then
Sleep() ;
如果精度要求高,千万不要用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;
}
大概就是这样子的。