控制台程序中使用定时器
来源:互联网 发布:淘宝短信催付款话术 编辑:程序博客网 时间:2024/05/22 15:24
转1
http://www.cnblogs.com/phinecos/archive/2008/03/08/1096691.html
我现在项目是一个控制台程序,用到的Win32API都是与界面无关的,今天需要加入定时器刷新的功能,由于没有消息循环,所以WM_TIMER消息应该如何处理呢?综合了下网上找到的资料,写了个简单的demo,个人以为这种在一个线程中创建定时器,再通过指定的回调函数来处理定时器触发的模式是比较好的。
#include <windows.h>
#include <stdio.h>
#include <conio.h>
int count =0;
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
}
DWORD CALLBACK Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,TimerProc);
BOOL bRet;
while( (bRet = GetMessage(&msg,NULL,0,0))!=0)
{
if(bRet==-1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
int main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application\n");
HANDLE hThread = CreateThread(NULL,0,Thread,0,0,&dwThreadId);
_getch();
return 0;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
GetTickCount可以到18-20ms精度
timeGetTime可以到1ms精度
QueryPerformanceFrequency 配合 QueryPerformanceCounter可以做到很高的精度
#include <windows.h>#include <iostream>//after call MyTimeProce MAX_TIMER_CALLED times, kill the timer and quit the application const int MAX_TIMER_CALLED = 100;//timer procedurevoid CALLBACK MyTimerProc( HWND hwnd, UINT uMsg,UINT_PTR idEvent,DWORD dwTime );//console application entry pointint main(){ // Set the timer. UINT_PTR IDT_TIMER = 1; // timer identifier SetTimer( NULL, // handle to main window IDT_TIMER, // timer identifier 100, // 1-second interval (TIMERPROC) MyTimerProc); // timer callback // we need a message loop to deal with the timer event MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0;}// implementation of MyTimerProcvoid CALLBACK MyTimerProc( HWND hwnd, UINT uMsg,UINT_PTR idEvent,DWORD dwTime ){ static int nCalled = 0; ++nCalled; std::cout << "MyTimerProc called " << nCalled << " times" << std::endl; if ( nCalled >= MAX_TIMER_CALLED ) { KillTimer(hwnd, idEvent); // kill the timer PostMessage(NULL, WM_QUIT, 0, 0); // quit the application }}
0 0
- 控制台程序中使用定时器
- VC++控制台程序中使用定时器
- C++控制台程序中使用定时器
- C++控制台程序中使用定时器
- VC++控制台程序中使用定时器
- VC++控制台程序中使用定时器
- VC++控制台程序中使用定时器
- 在Win32控制台程序中使用定时器
- VC++控制台程序中使用定时器
- 邢红瑞的blog--如何在控制台程序中使用定时器
- C#定时器【控制台程序】
- 控制台程序中使用CString
- 定时器SetTimer如何用在win32控制台用用程序中
- 在非控制台程序中使用控制台来调试程序。
- c++控制台程序实现定时器
- c++控制台程序实现定时器
- c++控制台程序实现定时器
- 在控制台程序中不能使用CAsyncSocket
- HTTP 2.0要来了,你竟然还不知道什么是SPDY?
- Thread Looper Handler
- Requests:Python HTTP Module学习笔记(一)
- 《Python基础教程》第6~8章 学习笔记
- adb logcat技巧
- 控制台程序中使用定时器
- Matlab--JDBC建立到Oracle数据库的连接
- 比urllib好用的requests
- 值得关注议题:语音识别-框架与技术
- [DFS+剪枝]POJ 1011/HDOJ 1455/HOJ 1049 Sticks
- linux debugfs使用---软件查看gpio状态
- 如何让非root用户启用小于1024号的端口
- 图的广度优先遍历,基于邻接链表实现
- 在 Windows 下为 PHP 5.4 安装 PEAR、PHPUnit 及 phpDoc2