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. location.href的用户总结

    *.location.href 使用方法: top.location.href="url"          在顶层页面打开url(跳出框架) self.location.href ...

  2. 使用systemtap调试linux内核

    http://blog.csdn.net/heli007/article/details/7187748 http://linux.chinaunix.net/docs/2006-12-15/3479 ...

  3. [转] JavaScript 和事件

    与浏览器进行交互的时候浏览器就会触发各种事件.比如当我们打开某一个网页的时候,浏览器加载完成了这个网页,就会触发一个 load 事件:当我们点击页面中的某一个“地方”,浏览器就会在那个“地方”触发一个 ...

  4. Java基础知识强化之集合框架笔记26:LinkedList的特有功能

    1. LinkedList的特有功能: (1)添加功能  public  void  addFirst(Object   e)  public  void  addLast(Object   e) ( ...

  5. Objective-C:KVO

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  6. HTML的Get方法URL传递中文参数,解决乱码问题

    本例中有使用JQuery. 资料参考:http://www.cnblogs.com/babycool/p/3169058.html 发送的HTML页面代码: <!DOCTYPE html> ...

  7. jquery parseInt()的问题

    对于parseInt("01")到parseInt("07");都能得到正确的结果,但如果是parseInt("08") 或parseInt ...

  8. insert当 sql语句里面有变量 为字符类型的时候 要3个单引号

    set @InsertStr='INSERT INTO [dbo].[T_SchoolPercentMonth]([SchoolID],[MonthOfYear],[PercentNum]) VALU ...

  9. object转化为string

    package common; import java.util.ArrayList; import java.util.List; public class DataZh { public stat ...

  10. 我的博客css得到别人的认可

    你好 在吗?? 本消息来自QQ临时会话,如果您收到骚扰信息,可点击以下网址中的"停用服务"关闭临时会话: http://shang.qq.com/widget/set.php 20 ...