定时器也不准?
我后来也 提升了 线程和进程优先级。。。class _2nDs_Timer
{
private:
HANDLE hTimer;
public:
_2nDs_Timer(unsigned char bInit = 0);
~_2nDs_Timer(void);
unsigned char CreateTimer(void); // 返回负数表示创建失败
unsigned char DeleteTimer(void);
unsigned char IsExist(void);
unsigned char WaitTime(int Itime); // 毫秒
unsigned int GetTime(void);//毫秒
};
_2nDs_Timer::_2nDs_Timer(unsigned char bInit)
{
DWORD dwCurrentProcessID = GetCurrentProcessId();
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwCurrentProcessID);
DWORD dwCurrentThreadID = GetCurrentThreadId();
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dwCurrentThreadID);
if (hProcess)
printf("打开进程成功.\n");
else
printf("打开进程失败.\n");
if (hThread)
printf("打开线程成功.\n");
else
printf("打开线程失败.\n");
if (SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS))
printf("设置进程优先级成功.\n");
else
printf("设置进程优先级失败.\n");
if (SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL))
printf("设置线程优先级成功.\n");
else
printf("设置线程优先级失败.\n");
if (0 == bInit)
hTimer = 0;
else
hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
}
_2nDs_Timer::~_2nDs_Timer(void)
{
if (hTimer) CloseHandle(hTimer);
}
unsigned char _2nDs_Timer::CreateTimer(void)
{
if (hTimer) return 0x10;
hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
if (NULL == hTimer) return 0x20;
return 1;
}
unsigned char _2nDs_Timer::DeleteTimer(void)
{
if (NULL == hTimer) return 0x10;
if (FALSE == CloseHandle(hTimer)) return 0x20;
hTimer = 0;
return 1;
}
unsigned char _2nDs_Timer::IsExist(void)
{
if (hTimer)
return 1;
return 0;
}
unsigned char _2nDs_Timer::WaitTime(int Itime)
{
if (NULL == hTimer) return 0;
if (WaitForSingleObject(hTimer, (DWORD)Itime))
return 1;
return 0;
}
unsigned int _2nDs_Timer::GetTime(void)
{return (UINT)GetTickCount();}
main中
_2nDs_Timer timer(1);
while(1)
{
time = timer.GetTime();
timer.WaitTime(16);
int intime = timer.GetTime() - time;
printf("time_delta : %d\n", intime);
}
结果。。。
15
16
15
16
就是15 或者16
但是 timer.WaitTime(16);
就会显示
31
32
31
32
就是 32或者31
timer.WaitTime(8);
0、15、16都有可能。。。
是CPU中断频率的作用么?? UP。。。。 用QueryPerformanceCounter和QueryPerformanceFrequncy做计时器吧,这是CPU时间,100%准确的 -w- QueryPerformanceCounter和QueryPerformanceFrequncy可以让进程不占用CPU不?
一个是计数,一个是频率,难道要死循环做判断?
页:
[1]