能力值:
(RANK:1010 )
2 楼
如果需要取得精确的系统时间,使用QueryPerformanceFrequency和QueryPerformanceCounter
如果需要精确定时例如1ms,使用等待定时器的APC机制( CreateWaitableTime/SetWaitableTime)
能力值:
( LV2,RANK:10 )
3 楼
谢谢老大指点,我试了APC机制,但是在定时20ms间隔时他的误差会在一两毫秒间,我需要的最好能在1ms内,不知是不是程序的问题,请老大指正一下。
CreateWaitableTime/SetWaitableTime
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
#define _SECOND 10000000
typedef struct _MYDATA {
TCHAR *szText;
DWORD dwValue;
} MYDATA;
VOID CALLBACK TimerAPCProc(
LPVOID lpArg, // Data value
DWORD dwTimerLowValue, // Timer low value
DWORD dwTimerHighValue ) // Timer high value
{
MYDATA *pMyData = (MYDATA *)lpArg;
SYSTEMTIME st;
GetLocalTime(&st);
printf( "Message: %s\nValue: %d\n\n", pMyData->szText,
pMyData->dwValue );
printf("st.wSecond=%d,st.wMilliseconds=%d\n",st.wSecond,st.wMilliseconds);
MessageBeep(0);
}
void main( void )
{
HANDLE hTimer;
BOOL bSuccess;
__int64 qwDueTime;
LARGE_INTEGER liDueTime;
MYDATA MyData;
TCHAR szError[255];
MyData.szText = "This is my data.";
MyData.dwValue = 100;
if ( hTimer = CreateWaitableTimer(
NULL, // Default security attributes
FALSE, // Create auto-reset timer
"MyTimer" ) ) // Name of waitable timer
{
__try
{
qwDueTime = -2 * _SECOND;
liDueTime.LowPart = (DWORD) ( qwDueTime & 0xFFFFFFFF );
liDueTime.HighPart = (LONG) ( qwDueTime >> 32 );
bSuccess = SetWaitableTimer(
hTimer, // Handle to the timer object
&liDueTime, // When timer will become signaled
10, // Periodic timer interval of 2 seconds
TimerAPCProc, // Completion routine
&MyData, // Argument to the completion routine
FALSE ); // Do not restore a suspended system
if ( bSuccess )
{
for ( ; MyData.dwValue < 1000; MyData.dwValue += 100 )
{
SleepEx(
INFINITE, // Wait forever
TRUE ); // Put thread in an alertable state
}
}
else
{
wsprintf( szError, "SetWaitableTimer failed with Error \
%d.", GetLastError() );
MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );
}
}
__finally
{
CloseHandle( hTimer );
}
}
else
{
wsprintf( szError, "CreateWaitableTimer failed with Error %d.",
GetLastError() );
MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );
}
}
能力值:
( LV2,RANK:10 )
4 楼
请教一下老大们,在WINXP下是否还可以得到原来的INT8时钟中断,APIC里面的好像没有了,哪位老大能指导一下.