Windows 下精确时间控制

博客聚焦于Windows系统下的精确时间控制,在信息技术领域,精确时间控制对系统运行、程序执行等有重要意义,能保障系统的稳定性和程序的准确性。


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;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值