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. shell 获取网关 以及修改ip 启用网卡

    shell 获取网关 以及修改ip 启用网卡 #!/bin/bash #autho freefei #script is a init computer eth #data 2014 10 09 19 ...

  2. HDU 1025 Constructing Roads In JGShining&#39;s Kingdom (DP)

    Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...

  3. rabbitmq 消息持久化之receive and send

    二: 任务分发 &消息持久化   启用多个接收端的时候如果某一个receive 关闭要保证消息有反馈是否收到   send端 #-*- coding: UTF-8 -*-import pika ...

  4. EF FluentAPI映射一对多 关系时候报错

    提示很明显,不可为空的外键为空了,但是 问题是,我只是初始化 关系映射而已:见代码 public ColumnsCategoryMapConfiguration() { ToTable("C ...

  5. ubuntu桌面变空白,或者只有壁纸,任务栏消失的解决办法

    原因:因为打开了桌面特效的原因,但设置不合导致的. 解决方法:方法一:1.按住Ctrl+Alt+F1切换到字符终端下,输入用户名和密码登录2.输入以下命令删除出错的Compiz配置文件相关目录:rm ...

  6. youphp学习整理

    <?php //后台公共模块 // _list 数据显示 // add 添加/编辑 视图 // insert 添加处理函数 // edit 添加/编辑 视图 // update 更新处理函数 / ...

  7. 在Iframe框架下如何跳转到登录界面

    在Iframe框架下跳转到登录界面总会跳到子界面中,类似于下图 试用Respon.Redirect()不行, 用Js函数,但我跳转代码都是写在cs文件中的,用Respose.write(),js函数根 ...

  8. [转]mysql导出导入中文表解决方法

    在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...

  9. c语言字符数组和指针的经典用法

    1.字符数组 许多情况下,对字符串的处理使用字符数组会更加方便,比如: 我觉得不改变字符串的原有顺序,对字符串进行删除等操作时,使用字符数组效果会更好. eg:给定字符串(ASCII码0-255)数组 ...

  10. apple iphone 3gs 有锁机 刷机 越狱 解锁 全教程(报错3194,3014,1600,短信发不出去等问题可参考)

    以自身经历列步骤如下:(基本思路就是刷6.1.6,越狱,降级基带,解锁) 一.准备工作 1.下载3gs 6.1.6官方固件.地址:http://act.feng.com/wetools/index.p ...