使用timesetevent,时间间隔33ms, 每秒将产生10ms的误差。

解决方案 »

  1.   

    Windows 好像没有即时消息的...
      

  2.   

    精确计时的类#if !defined(TIMETICKER_H)
    #define TIMETICKER_H// TimeTick.h : interface of the CTimeTick class
    //
    /////////////////////////////////////////////////////////////////////////////
    //
    // Copyright ?2001, Stefan Belopotocan, http://welcome.to/BeloSoft
    //
    /////////////////////////////////////////////////////////////////////////////#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
    // CTimeTickclass CTimeTick
    {
    CTimeTick(const CTimeTick& d);
    CTimeTick& operator=(const CTimeTick& d);public:
    CTimeTick();
    ~CTimeTick(); // Operations
    void Start();
    float Tick();
    bool  isPerformanceCounter() {return m_nPerformanceFrequency;} // Implementation
    protected:
    static __int64 GetPerformanceFrequency();
    static float GetTimeInMilliSeconds(__int64 nTime); // Data
    private:
    static __int64 m_nPerformanceFrequency; LARGE_INTEGER m_nTimeElapsed;
    LARGE_INTEGER m_nTime;
    };#endif // !defined(TIMETICKER_H)
    // TimeTick.cpp : implementation of the CTimeTick class
    //
    /////////////////////////////////////////////////////////////////////////////
    //
    // Copyright ?2001, Stefan Belopotocan, http://welcome.to/BeloSoft
    //
    /////////////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "TimeTick.h"#ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif
    //////////////////////////////////////////////////////////////////////
    // CTimeTick__int64 CTimeTick::m_nPerformanceFrequency = CTimeTick::GetPerformanceFrequency();CTimeTick::CTimeTick()
    {
    m_nTimeElapsed.QuadPart = 0;
    m_nTime.QuadPart      = 0;
    }CTimeTick::~CTimeTick()
    {
    }void CTimeTick::Start()
    {
    if (m_nPerformanceFrequency)
    QueryPerformanceCounter(&m_nTimeElapsed);
    m_nTime.QuadPart = 0;
    }float CTimeTick::Tick()
    {
    LARGE_INTEGER nTime; if (m_nPerformanceFrequency){
    QueryPerformanceCounter(&nTime); float nTickTime = GetTimeInMilliSeconds(nTime.QuadPart - m_nTimeElapsed.QuadPart);
    m_nTimeElapsed.QuadPart = nTime.QuadPart; return nTickTime;
    }
    return 0.0f;
    }__int64 CTimeTick::GetPerformanceFrequency()
    {
    LARGE_INTEGER nPerformanceFrequency; if (QueryPerformanceFrequency(&nPerformanceFrequency))
    return nPerformanceFrequency.QuadPart;
    else
    return 0;
    }float CTimeTick::GetTimeInMilliSeconds(__int64 nTime)
    {
    return ((float) (nTime*1000i64)) / ((float) m_nPerformanceFrequency);
    }
      

  3.   

    Windows不是一个实时操作系统。在多任务操作系统中你无法保证你的程序在指定的时间会被执行。你可以用QueryPerformanceFrequency和QueryPerformancecounter来获得高精度的时间,但是你的程序的优先级会决定你的时间查询代码什么时候被执行。
      

  4.   

    用QueryPerformanceFrequency()和 QueryPerformanceCounter()函数误差能达到微秒级
    这两个函数是VC提供的仅供Windows 95及其后续版本使用的精确时间函数,其精度与CPU的时钟频率有关,并要求计算机从硬件上支持精确定时器
      

  5.   

    windows的时间不可能没有误差!!!!