VC++控制台程序中使用定时器
来源:互联网 发布:江浦教育行知实践基地 编辑:程序博客网 时间:2024/05/22 06:20
http://blog.chinaunix.net/uid-20437338-id-1946459.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;
}
#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://blog.csdn.net/markman101/article/details/6220295
#include "stdafx.h"
#include<iostream>
#include<windows.h>
using namespace std;
void CALLBACK TimeProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime);
int main()
{
int sen = SetTimer(NULL,1,1000,TimeProc);
//Sleep(5000);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
if(msg.message==WM_TIMER)
{
DispatchMessage(&msg);
cout<<"kill定时器!"<<endl;
KillTimer(NULL, sen);
}
}
return 0;
}
void CALLBACK TimeProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime)
{
cout<<"触发定时器!"<<endl;
}
#include<iostream>
#include<windows.h>
using namespace std;
void CALLBACK TimeProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime);
int main()
{
int sen = SetTimer(NULL,1,1000,TimeProc);
//Sleep(5000);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
if(msg.message==WM_TIMER)
{
DispatchMessage(&msg);
cout<<"kill定时器!"<<endl;
KillTimer(NULL, sen);
}
}
return 0;
}
void CALLBACK TimeProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime)
{
cout<<"触发定时器!"<<endl;
}
0 0
- VC++控制台程序中使用定时器
- VC++控制台程序中使用定时器
- VC++控制台程序中使用定时器
- VC++控制台程序中使用定时器
- VC++控制台程序中使用定时器
- 控制台程序中使用定时器
- C++控制台程序中使用定时器
- C++控制台程序中使用定时器
- 在Win32控制台程序中使用定时器
- 邢红瑞的blog--如何在控制台程序中使用定时器
- VC 控制台程序中隐藏控制台窗口
- vc中调用控制台程序
- VC的Win32控制台程序中使用MFC库文件
- vc中定时器的使用
- vc中定时器的使用
- VC中定时器的使用
- VC中定时器的使用
- VC中定时器的使用
- ios8来了,屏幕更大,准备好使用 iOS Auto Layout了吗?
- iOS 自定义scrollView宽度,分页滑动
- [数据结构与算法]最小生成树(普利姆算法、克鲁斯卡尔算法)
- Xcode6的新特性、iPhone6和iPhone6Plus的适配
- Lua单例
- VC++控制台程序中使用定时器
- Mybatis-3动态代理来映射配置文件原理
- fhyiiuiuiiouiouiou
- hbase多表输出
- php 不转义中文字符的 json 编码方法
- 王立平--ProgressDialog
- 关于自动布局(Autolayout)
- 要个嵌入式开发板做什么?
- 像素、英寸、厘米之间的换算关系