Windows 下精确时间控制
[以下文章是从我自己在MSN SPACE上搬过来的,那边的blog太难用,这边的其实也不好用 :D]
I'm totally occupied recently.... :-( , i just can't spare out to type a word on the blog...
for measuring the time accuratly, I wrote double classes and share it...
copy too much code from CodeProject.com, so i contribute some...LOL
实在太忙……
最近要编写一个实时波形绘图程序,对时间要求比较高,研究了一下,写出两个类.共享源码...
StopWatch
class MMTimer;
Description:
the class StopWatch for measuring the duration accuratly, 1ms differency.
the class MMTimer for triggering a callback function periodicly...
Example:
1)
StopWatch watch;
watch.Start();
//....your code to measure...
watch.Stop();
double duration = watch.Duration(); // in milliseconds
2)
MMTimer<CDialog1> timer;
timer.Start(10, this, &CDialog1::YourVoidProc);
...
代码:
#pragma once
#include <winbase.h>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 精确测量时间,误差为1ms
//-------------------------------
class StopWatch
{
public:
StopWatch() : m_nFrequency(0)
{
if (QueryPerformanceFrequency(&m_query))
m_nFrequency = m_query.QuadPart;
Start();
}
void Start()
{
QueryPerformanceCounter(&m_query);
m_nPrevCount = m_query.QuadPart;
}
void Stop()
{
QueryPerformanceCounter(&m_query);
m_nCurrCount = m_query.QuadPart;
}
//返回毫秒数
double Duration()
{
//硬件不支持
if (!m_nFrequency)
return -1;
return 1000 * (double)(m_nCurrCount - m_nPrevCount) / m_nFrequency;
}
private:
__int64 m_nFrequency;
__int64 m_nPrevCount;
__int64 m_nCurrCount;
LARGE_INTEGER m_query;
};
//=================================================
//----------------------source code [MMTimer.h]-----------------------------------------------------
#pragma once
#pragma comment(lib, "winmm.lib")
#include
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Usage: MMTimer timer(this);
// timer.Start(3,this, &YourCallCalssName::MethodName);
//
//-----------------------------------------------------------
template<typename T>
class MMTimer
{
public:
typedef void (T::*CallBackProc)();
MMTimer() : m_nResolution(0), m_nTimerId(NULL), m_lpCallFrom(NULL)
{
TIMECAPS tc;
if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) == TIMERR_NOERROR)
{
//分辨率越小越精确
m_nResolution = tc.wPeriodMin;
timeBeginPeriod(m_nResolution);
}
}
bool Start(int nDelay, T* lpCallFrom, CallBackProc lpCallBackProc)
{
m_lpCallFrom = lpCallFrom;
m_callback = lpCallBackProc;
m_nTimerId = timeSetEvent(nDelay, m_nResolution, callback, (DWORD_PTR)this, TIME_PERIODIC);
return m_nTimerId != NULL;
}
bool Stop()
{
if (m_nTimerId == 0)
return true;
MMRESULT result = timeKillEvent(m_nTimerId);
if (result == TIMERR_NOERROR)
m_nTimerId = 0;
return result == TIMERR_NOERROR;
}
~MMTimer()
{
Stop();
if (m_nResolution != 0)
timeEndPeriod(m_nResolution);
}
private:
static void CALLBACK callback(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
MMTimer<T>* lpTimer = (MMTimer<T>*)dwUser;
(lpTimer->m_lpCallFrom->*(lpTimer->m_callback))();
}
private:
CallBackProc m_callback;
T* m_lpCallFrom;
unsigned int m_nResolution;
unsigned int m_nTimerId;
};
博客聚焦于Windows系统下的精确时间控制,在信息技术领域,精确时间控制对系统运行、程序执行等有重要意义,能保障系统的稳定性和程序的准确性。
513

被折叠的 条评论
为什么被折叠?



