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. docker 实战---部署tomcat\weblogic集群 (二)

    安装tomcat镜像 准备好须要的jdk tomcat等软件放到home文件夹以下 启动一个虚拟机 docker run -t -i -v /home:/opt/data  --name mk_tom ...

  2. android 44 SQLiteOpenHelper

    java package com.sxt.day06_10; import java.util.ArrayList; import com.sxt.day06_10.entity.StudentBea ...

  3. LINUX 中的 TCP/IP协议 参数详解

    Ipsysctl tutorial 1.0.4 Prev Chapter 3. IPv4 variable reference Next https://www.frozentux.net/ipsys ...

  4. Qt 学习之路 2(80):定位器

    QML 提供了很多用于定位的元素.这些元素叫做定位器,都包含在 QtQuick 模块.这些定位器主要有 Row.Column.Grid和Flow等. 为了介绍定位器,我们先添加三个简单的组件用于演示: ...

  5. SpringMVC + Spring 3.2.14 + Hibernate 3.6.10 集成详解

    注:此文档只说明简单的框架集成,各个框架的高级特性未涉及,刚刚接触框架的新人可能需要参考其他资料. PS:本次练习所用jar包都能在此下载到:http://pan.baidu.com/s/1sjmgd ...

  6. Node.js + Express + Mongodb 开发搭建个人网站(一)

    一.Node + Express环境搭建 0.去Node官网下载安装node,如果安装了 npm 和 node的话 那么就 安装 全局的 express,-g全局安装 npm install expr ...

  7. java构造函数也可以用private开头

    private 构造函数一般用于Singleton模式,指的是整个应用只有本类的一个对象,一般这种类都有一个类似getInstance()的方法!下面是一个Singleton的例子:public cl ...

  8. .NET Core跨平台开发

    对于.NET开源计划想必关注的人已经跃跃欲试了,但是真正将其用于开发的目前来说不多.毕竟截至本文发布时.NET Core才发布到1.0RC2版本.正式版预计还有一段时间.况且大多数人都是持观望态度,就 ...

  9. .Net framework.

    Figure 1 - .Net Framework The Common Language Runtime (CLR) is the mechanism through which .NET code ...

  10. Unity Manual 用户手册

    unity3d 文档的中文网址:   http://game.ceeger.com/Manual/