C++控制台程序中使用定时器
转自博客:http://www.cnblogs.com/phinecos/archive/2008/03/08/1096691.html
作者:洞庭散人
“我现在项目是一个控制台程序,用到的Win32API都是与界面无关的,今天需要加入定时器刷新的功能,由于没有消息循环,所以WM_TIMER消息应该如何处理呢?综合了下网上找到的资料,写了个简单的demo,个人以为这种在一个线程中创建定时器,再通过指定的回调函数来处理定时器触发的模式是比较好的。”
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)
{
printf("Error:the thread will quit,error id is %d/n",GetLastError());
break;
}
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,NULL,0,NULL);
_getch();
return 0;
};
本人在了解了作者的意图以后,也做了一个类封装:
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
class CTimer
{
public:
CTimer();
void CreateTimerThread(int* pi);
static DWORD CALLBACK TimeThread(PVOID pvoid);
static void CALLBACK TimeProc(HWND hWnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);
};
CTimer::CTimer()
{
}
void CTimer::CreateTimerThread(int* pi)
{
HANDLE hand = CreateThread(NULL,0,CTimer::TimeThread,pi,0,NULL);
}
DWORD CALLBACK CTimer::TimeThread(PVOID pvoid)
{
int* pi = (int*)pvoid;
int itm = *pi;
MSG msg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
UINT timeid = SetTimer(NULL,111,itm,CTimer::TimeProc);
BOOL bRet;
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,timeid);
printf("thread end here/n");
return 0;
}
void CALLBACK CTimer::TimeProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
printf("WM_TIMER in work thread count/n");
}
int main()
{
int iTime = 1000;
int* pi = &iTime;
CTimer* ptime = new CTimer;
ptime->CreateTimerThread(pi);
_getch();
return 0;
};
感谢洞庭散人...
C++控制台程序中使用定时器的更多相关文章
- 在.NET Core控制台程序中使用依赖注入
之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...
- 如何在.NET Core控制台程序中使用依赖注入
背景介绍 依赖注入(Dependency Injection), 是面向对象编程中的一种设计原则,可以用来减低代码之间的耦合度.在.NET Core MVC中 我们可以在Startup.cs文件的Co ...
- 让你提前认识软件开发(21):C程序中的定时器
版权声明:本文为博主原创文章.对文章内容有不论什么意见或建议.欢迎与作者单独交流.作者QQ(微信):245924426. https://blog.csdn.net/zhouzxi/article/d ...
- 在控制台程序中,添加config文件
一.右击类库 → 添加 → 新建项 → 应用程序配置文件(或者选择一个XML文件,然后将名字改成XXX.config),内容如下: <?xml version="1.0" e ...
- 将CodedUI Test 放到控制台程序中,模拟鼠标键盘操作
CodedUI Test是微软的自动化测试工具,在VS中非常好用.可以用来模拟鼠标点击,键盘输入.但执行的时候必须要用mstest调用,无法传入参数(当然可以写入config文件中,但每次修改十分麻烦 ...
- 在C语言控制台程序中播放MP3音乐
游戏没有声音多单调. 这里做一个简单的范例,用 mciSendString 函数播放 MP3 格式的音乐,先看看代码吧: // 编译该范例前,请把 background.mp3 放在项目文件夹中 // ...
- .net core控制台程序中使用原生依赖注入
如果要在程序中使用DbContext,则需要先在Nuget中安装Microsoft.EntityFrameworkCore.SqlServer using ConsoleApp1.EntityFram ...
- C#控制台程序中处理2个关闭事件的代码实例
我们开发的控制台应用,在运行阶段很有可能被用户Ctrl+C终止或是被用户直接关闭.如果我们不希望用户通过Ctrl+C终止我们的程序,就需要对Ctrl+C或关闭事件作处理. 处理方法 在.net平台下C ...
- 9 在C#控制台程序(console)中让用户输入
经过前面那些练习,我们已经熟悉录入一些简单的代码.这些代码可以进行一些简单的运算,在dos窗口打印出一些东西出来.我们现在要开始学习如何把数据从外部输入到我们的程序中. 其实大多数程序的工作是完成下面 ...
随机推荐
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- jQuery导航菜单防刷新
为了实现最主要的功能,只写了一个粗糙的案例 CSS样式 ul,li{ list-style-type:none;} .nav { width: 100%; height: 35px; line-hei ...
- java Spring 基于注解的配置(一)
注解引用:1.service.xml 配置注解模式 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- linux mysql 安装(rpm)
linux上安装mysql, 就需要两个文件, xx.client.xx.rpm和 xx.server.xx.rpm 如 MySQL-client-community-5.1.72-1.rhel5.i ...
- 前不久一个swift项目用uicollectionview 用sdwebimage 加载图片,发生内存猛增,直接闪退的情况,简单说一下解决方案。
1.首先在appdelegate方法 didFinishLaunchingWithOptions SDImageCache.sharedImageCache().maxCacheSize=1024*1 ...
- MVVM学习笔记
MVVM学习笔记 1.MVVM的简介 MVVM模式是Model-View-ViewModel模式的简称,也就是由模型(Model).视图(View).视图模型(ViewModel),其目的是为了实现将 ...
- ZOJ 2745 01-K Code(DP)(转)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1745 题目大意:一个串由N个字符组成,每个字符是‘0’或者是‘1’, ...
- POJ 3311 Hie with the Pie(DP状态压缩+最短路径)
题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...
- Lua数组排序
代码 network = { {name = "grauna", IP = "210.26.30.34"}, {name = "arraial&quo ...
- 九度OJ 1042 Coincidence -- 动态规划(最长公共子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1042 题目描述: Find a longest common subsequence of two strings ...