命令行以及DLL程序用SetTimer实现定时器
SetTimer是Windows程序下的设置定时器用的,通过SetTimer设置定时器之后,系统会向窗口发送WM_TIMER消息。系统通过响应WM_TIMER消息来执行代码。但是通过在命令行程序或者DLL里面,没有窗口,所以收不到系统发送的WM_TIMER,所以,这个时候,就要使用另外一种方式,那就是自己建立消息循环。下面是一个命令行程序的示例,基本显示了如何在命令行以及DLL程序如何用SetTimer实现定时器:
#include "stdafx.h" #include <windows.h> #include <iostream> using namespace std; static void getdata(); void main() { MSG msg; SetTimer(NULL, 0, 1000, (TIMERPROC)getdata); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } void getdata() { cout << "hello world!" << endl; }
近期评论