Timers (SetTimer and CreateWaitableTimer) in Windows

 

SetTimer

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 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) { int iTimerID = SetTimer(0, 0, 300, foo_class::timer_proc);
MSG m;
while (GetMessage(&m,0,0,0)) {
TranslateMessage(&m);
DispatchMessage(&m); }
return 1;
}

CreateWaitableTimer

This example demonstrates how to use Timers in windows. A timer will be set that is signalled for the first time 2 seconds after the first call to CreateWaitableTimer and then is signalled every 3/4th of a second.
#define _WIN32_WINNT 0x0400

#include <windows.h>
#include <process.h>
#include <stdio.h> unsigned __stdcall TF(void* arg) {
HANDLE timer=(HANDLE) arg; while (1) {
WaitForSingleObject(timer,INFINITE);
printf(".");
} } int main(int argc, char* argv[]) { 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 li.QuadPart=-(2*unitsPerSecond);
SetWaitableTimer(
timer,
&li,
750, // Set the event every 750 milli Seconds
0,
0,
false); _beginthreadex(0,0,TF,(void*) timer,0,0); // Wait forever,
while (1) ; return 0;
}

http://www.adp-gmbh.ch/win/misc/timer.html

SetTimer and CreateWaitableTimer的例子(静态函数设置为回调函数,瑞士的网页,有点意思)的更多相关文章

  1. C++临时变量的回顾思考以及librdkafka设置回调函数注意点

    1 生命周期 如果仅仅是临时变量,并没有调用new来在堆上创建空间,那么注意 : 生命周期仅在该作用域中,即声明该临时变量的{}中: 2 使用(librdkafka C++回调使用) 在创建临时变量后 ...

  2. SetTimer 与 回调函数

    在控制台应用程序中,SetTimer的函数原型为: UINT_PTR SetTimer( HWND hWnd, // handle to window UINT_PTR nIDEvent, // ti ...

  3. js的回调函数 一些例子

    这边用bootstrap 3.0的  上传控件做例子 下面是上传控件的一段完整的 js 操作 代码. <!-- 上传缩略图控件配置 --><script> // 定义这四个全局 ...

  4. C++中 线程函数为静态函数 及 类成员函数作为回调函数

    线程函数为静态函数: 线程控制函数和是不是静态函数没关系,静态函数是在构造中分配的地址空间,只有在析构时才释放也就是全局的东西,不管线程是否运行,静态函数的地址是不变的,并不在线程堆栈中static只 ...

  5. CKFinder 弹出窗口操作并设置回调函数

    CKFinder 弹出窗口操作并设置回调函数 官方例子参考CKFinderJava-2.4.1/ckfinder/_samples/popup.html 写一个与EXT集成的小例子 Ext.defin ...

  6. Qt 学习之路 2(19):事件的接受与忽略(当重写事件回调函数时,时刻注意是否需要通过调用父类的同名函数来确保原有实现仍能进行!有好几个例子。为什么要这么做?而不是自己去手动调用这两个函数呢?因为我们无法确认父类中的这个处理函数有没有额外的操作)

    版本: 2012-09-29 2013-04-23 更新有关accept()和ignore()函数的相关内容. 2013-12-02 增加有关accept()和ignore()函数的示例. 上一章我们 ...

  7. 通过修改i8042prt端口驱动中类驱动Kbdclass的回调函数地址,达到过滤键盘操作的例子

    同样也是寒江独钓的例子,但只给了思路,现贴出实现代码 原理是通过改变端口驱动中本该调用类驱动回调函数的地方下手 //替换分发函数 来实现过滤 #include <wdm.h> #inclu ...

  8. soui使用wke时,设置js回调注意事项

    wke响应网页js函数调用时注意: 必须等网页加载完成后,才能通过SetJsFunc设置js函数与c++回调的对应.网页未加载就设置,不会响应c++函数. 示例代码: wkeJSData* data ...

  9. 转:Delphi 回调函数及例子

    http://anony3721.blog.163.com/blog/static/5119742010866050589/ { http://anony3721.blog.163.com/blog/ ...

随机推荐

  1. [PWA] Keynote: Progressive Web Apps across all frameworks

    PWA: Add to home screen Angular Universal Server side rendering: for achieving better proference on ...

  2. [Angular + Unit] AngularJS Unit testing using Karma

    http://social.technet.microsoft.com/wiki/contents/articles/32300.angularjs-unit-testing-using-karma- ...

  3. 字符串 赋值 以及gets

    我们知道字符串用字符数组或用指针实现,但是在赋值的时候产生了不少疑惑 使用方法一: char a[ ]={"I LOVE YOU!"}; 但是以下这样就是错的: char a[20 ...

  4. android 30 下拉列表框:ArrayAdapter和Spinner.

    package com.sxt.day05_04; import android.os.Bundle; import android.app.Activity; import android.cont ...

  5. Java基础知识强化之集合框架笔记32:集合之可变参数的概述和使用

    1. 可变参数的概述和使用: (1)可变参数:定义方法的时候不知道该定义多少个参数(2)格式:     修饰符  返回值类型  方法名(数据类型… 变量名){   }  注意: 这里的变量其实是一个数 ...

  6. css 权威指南笔记(二)元素

    替换元素 用来替换元素内容的部分并非有文档内容直接表示. img input 非替换元素 其内容由用户代理(通常是一个浏览器)在元素本身生成的框中显示. 块级元素 块级元素生成一个 元素框,(默认)会 ...

  7. Restart-ServiceEx.psm1

    详细描述 利用WMI的Win32_Service类重启指定计算机上的服务. Restart-ServiceEx cmdlet 通过WMI的Win32_Service类向指定计算机(ComputerNa ...

  8. Javascript绝句欣赏

    1. 取整同时转成数值型: '10.567890'|0 结果: 10 '10.567890'^0 结果: 10 -2.23456789|0 结果: -2 ~~-2.23456789 结果: -2 2. ...

  9. struts_ognl详解

  10. reactjs 入门

    地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...