Windows中的计时器(SetTimer和CreateWaitableTimer)

 Windows中的计时器(SetTimer和CreateWaitableTimer)   
Timers (SetTimer and CreateWaitableTimer) in Windows
       
1.SetTimer
下面的例子创建了一个计时器(不与窗口相关联),该计时器过程函数建了20个消息框。
The following example creates a timer (that is not attached to a window) whose Timer Procedure creates 20 Message Boxes

#include <windows.h>

class foo_class {
  static int counter;
public:
  //static函数,相当于全局
  static void  __stdcall timer_proc(HWND,unsigned int, unsigned int, unsigned long) {
    if (counter++ < 20)
      MessageBox(0,"Hello","MessageBox",0);
    else
      PostQuitMessage(0);
  }
};

int foo_class::counter=0;

WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int) {

//第1个参数,MSDN中指出如果置为NULL,即0,不与窗口相关联。
//If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.
//第2个参数会被忽略
//第3个参数,300毫秒触发一次
//第4个参数,触发时由函数foo_class::timer_proc响应
int iTimerID = SetTimer(0, 0, 300, foo_class::timer_proc);
  MSG m;
//这是消息循环
  while (GetMessage(&m,0,0,0)) {
    TranslateMessage(&m);
    DispatchMessage(&m);

   }
  return 1;
}

2.CreateWaitableTimer
这个例子演示如何在windows中使用计时器。
计时器被设计为(1)在第1次调用CreateWaitableTimer后2秒触发,(2)此后每3/4秒触发一次。
#define _WIN32_WINNT 0x0400

#include <windows.h>
#include <process.h>
#include <stdio.h>

unsigned __stdcall TF(void* arg) {
  HANDLE timer=(HANDLE) arg;

  while (1) {
    //此处,进程间通信的接收方
    //timer是命名的,因此进程间或线程间没有区别
    WaitForSingleObject(timer,INFINITE);
    printf(".");
  }

}

int main(int argc, char* argv[]) {
  //创建,命名为0,也可以是LPCTSTR,字符串
  //其他进程可以通过OpenWaitableTimer获得此timer的句柄,并对之进行SetWaitableTimer
  HANDLE timer = CreateWaitableTimer(
    0,
    false, // false=>will be automatically reset
    0);    // name

  LARGE_INTEGER li;

  const int unitsPerSecond=10*1000*1000; // 100 nano seconds

  // Set the event the first time 2 seconds
  // after calling SetWaitableTimer
  //2秒
  li.QuadPart=-(2*unitsPerSecond);
  //通过句柄设置timer
  SetWaitableTimer(
    timer,
    &li,
    750,   // Set the event every 750 milli Seconds
    0,
    0,
    false);
  //用TF函数启动worker线程
  _beginthreadex(0,0,TF,(void*) timer,0,0);

  // Wait forever,
  while (1) ;

  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++,可以使用Windows APISetTimer函数来创建一个计时器,该函数的原型如下: ``` UINT_PTR SetTimer( HWND hWnd, // 窗口句柄 UINT_PTR nIDEvent, // 计时器标识符 UINT uElapse, // 计时间隔,单位为毫秒 TIMERPROC lpTimerFunc// 计时器回调函数 ); ``` 其,hWnd参数是要接收计时器消息的窗口句柄,nIDEvent是计时器的标识符,可以用于取消计时器,uElapse是计时间隔,单位是毫秒,lpTimerFunc是计时器回调函数,当计时器到达指定时间时,系统会发送一个WM_TIMER消息给指定窗口,同时调用回调函数。 以下是一个简单的示例代码,实现了每秒钟在窗口标题显示当前时间: ```c++ #include <Windows.h> #include <string> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: // 创建一个每秒钟触发一次的计时器 SetTimer(hWnd, 1, 1000, NULL); break; case WM_TIMER: if (wParam == 1) { // 获取当前时间 SYSTEMTIME st; GetLocalTime(&st); std::wstring title = L"Current Time: "; title += std::to_wstring(st.wHour) + L":" + std::to_wstring(st.wMinute) + L":" + std::to_wstring(st.wSecond); // 更新窗口标题 SetWindowText(hWnd, title.c_str()); } break; case WM_DESTROY: // 销毁计时器 KillTimer(hWnd, 1); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 注册窗口类 WNDCLASS wc = { 0 }; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = L"TimerDemo"; RegisterClass(&wc); // 创建窗口 HWND hWnd = CreateWindow(L"TimerDemo", L"Timer Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } // 显示窗口 ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 消息循环 MSG msg = { 0 }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } ``` 该示例代码,我们在窗口创建时创建了一个每秒钟触发一次的计时器,并在计时器回调函数获取当前时间,并将其显示在窗口标题。在窗口销毁时,我们销毁了计时器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值