Chinaunix首页 | 论坛 | 博客
  • 博客访问: 522347
  • 博文数量: 252
  • 博客积分: 6057
  • 博客等级: 准将
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-21 10:17
文章分类

全部博文(252)

文章存档

2013年(1)

2012年(1)

2011年(32)

2010年(212)

2009年(6)

分类: C/C++

2010-04-09 00:36:41

编译器:MinGW  GCC
在编译项目时,将winmm.dll库->MinGW\lib\libwinmm.a 这个文件添加到项目设置
中的linker Settings 中,将libwinmm.a的完整路径填入link librarires列表框
中。

#include
#include
#include

#define   ONE_MILL_SECOND  1
//定义时钟分辨率,以ms为单位
#define   TIMER_ACCURACY   1

using namespace std;

//定义时间间隔
uint32_t wTimerRes_1ms;
//定义系统定时器分辨率
uint32_t wAccuracy;
//定义定时器句柄
uint32_t m_TimerID_1ms;

bool myinitTimer(void)
{

  //给时间间隔变量赋值
  wTimerRes_1ms    = ONE_MILL_SECOND;

  TIMECAPS   tc;
  //利用函数timeGetDevCaps取出系统分辩率的取值范围,如果无错则继续并返回true,否则返回false
  if(timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR)
  {
    //分辨率的值不能超出系统的取值范围
    wAccuracy  = min((int)max((int)tc.wPeriodMin,TIMER_ACCURACY),(int)tc.wPeriodMax);
    //调用timeBeginPeriod函数设置定时器的分辨率
    timeBeginPeriod(wAccuracy);

    return true;
  }
  else
  {
    return false;
  }//end if(timeGetDevCaps(&ts,sizeof(TIMECAPS))==TIMER_NOERROR)

}

void CALLBACK timerProc(uint32_t wTimerID, uint32_t msg, uint64_t dwUser,
                        uint64_t dw1, uint64_t dw2)
{
  //1ms定时器的回调函数,类似于中断处理程序,一定要声明为全局PASCAL函数,否则编译会有问题

  //定义计数器
  static int ms = 0;
  //取得系统时间,以ms为单位
  uint64_t  osBinaryTime  = GetTickCount();
  //输出计数器值和当前系统时间
  cout<<++ms<<":1ms:"<
}

//启动1ms定时器
bool myStartMilliSecTimer(void)
{
  if((m_TimerID_1ms = timeSetEvent(wTimerRes_1ms,
                                   wAccuracy,
                                   //定时处理回调函数
                                   (LPTIMECALLBACK)timerProc,
                                   //用户传达室送到回调函数的数据
                                   NULL,
                                   //周期调用,只使用一次,用TIME_ONESHOT
                                   TIME_PERIODIC)) == 0)
  {
     cout<<"不能进行定时!"<     return false;
  }
  else
  {
    //不等于0表明加装成功,返回true

    cout<<"16ms 计 时:"<    return true;
  }//end if(m_TimerID_1ms = timeSetEvent()==0)
}

bool myStopMilliSecTimer(void)
{

  if(TIMERR_NOERROR == timeKillEvent(m_TimerID_1ms))
  {
      //删除以设置的分辨率
      timeEndPeriod(wAccuracy);
      cout<<"结束定时器!"<      return true;
  }
  else
  {
      cout<<"(X)无法结束定时器!"<      return false;
  }//end if(TIMERR_NOERROR == timeKillEvent())

}

int main()
{
    if(myinitTimer())
    {
      cout<<"定时器初始化成功!"<    }
    else
    {
      cout<<"(X)定时器初始化失败!"<    }//end if(myinitTimer())

    myStartMilliSecTimer();
    //system("pause");
    Sleep(1000);
    myStopMilliSecTimer();
    cout << "Hello world!" << endl;
    //system("pause");
    return 0;
}


修改debug:
将上面的代码中定时处理回调函数声名
由void CALLBACK timerProc(uint32_t wTimerID, uint32_t msg, uint64_t dwUser,
                        uint64_t dw1, uint64_t dw2)
改为 void CALLBACK timerProc(uint32_t wTimerID, uint32_t msg, uint32_t dwUser,
                        uint32_t dw1, uint32_t dw2)
因为 LPTIMECALLBACK数据结构的定义声名原型为:
typedef void(CALLBACK TIMECALLBACK)(UINT,UINT,DWORD,DWORD,DWORD);
其中 uint 其实就是uint32_t类型的,DWORD其实就是uint32_t类型的,不是uint64_t类型。
所以原来的函数声名会使程序运行时出现错误,因为有时无法找到定时处理回调函数过程。

程序运行载图:

程序源代码链接如下:
文件:mytestmmtimer_minGw.rar
大小:9KB
下载:下载





阅读(1019) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chulia200020012010-04-09 12:14:10

原创 timeSetEvent的用法(一) 收藏 url:[http://blog.csdn.net/xieyunc/archive/2009/04/29/4136125.aspx] 由于想在一个DLL中使用TTimer,想把它做成一个监控程序,然后采用DLL远