定时器SetTimer如何用在win32控制台用用程序中
2016-05-16 10:55
489 查看
SetTimer如何用在win32控制台应用程序中
可以使用在main函数中开一个子线程的方法,在子线程中用while(1){}的方式处理定时器的操作#include <Windows.h> #include <stdio.h> #include <conio.h> VOID CALLBACK TimerProc( HWND hwnd, // handle of window for timer messages UINT uMsg, // WM_TIMER message UINT idEvent, // timer identifier DWORD dwTime // current system time ) { static int s_count = 0; printf("WM_TIMER in work thread s_count = %d\n", ++s_count); } DWORD CALLBACK Thread(PVOID pvoid) { MSG msg; BOOL bRet; UINT timerid; PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); timerid = SetTimer(NULL, 0, 3000, TimerProc); while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) { if (bRet == -1) { printf("Error:the thread will quit,error id is %d\n", GetLastError()); break; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } KillTimer(NULL, timerid); printf("thread end heren"); return 0; } int main() { HANDLE hThread; printf("use timer in console application\n"); hThread = CreateThread(NULL, 0, Thread, NULL, 0, NULL); _getch(); return 0; }
相关文章推荐
- Linux信号(signal) 机制分析
- Linux信号(signal) 机制分析
- SetTimer在无窗口的使用注意事项
- MFC中SetTimer函数
- 中科院-杨力祥视频教程 07课程
- 【C++基础之八】函数指针和回调函数
- MFC Timer用法
- wxWidgets第十四课 wxTimer定时器
- C++定时器SetTimer实际ID说明
- win32控制台应用程序实现定时
- 定时器SetTimer如何使用在win32控制台应用程序中(2)
- chapter 3: 定时器的使用以及误解
- MFC之计时器
- 事件定时触发器 MFC(OnTimer,SetTimer,KillTimer)
- LNMP-php-fpm参数优化
- 使用 Spark Streaming 检测关键词
- WebStrom中的一些技巧和用法
- 线程通讯-传统方式
- 第二章 Stream API
- ym—— Android网络框架Volley(体验篇)