VC++控制台程序中使用定时器
2016-03-04 10:03
507 查看
#include <windows.h>
#include <stdio.h>
#include <conio.h>
int count=0;
VOID __stdcall TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if (count==10)
{
count=0;
system("cls");
}
}
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 __stdcall TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if (count==10)
{
count=0;
system("cls");
}
}
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;
}
相关文章推荐
- 《Effective C++》Rule 41: 了解隐式接口和编译期多态
- 关于C语言%u
- C语言位运算详解( 稳固下C吧)
- C语言——预编译
- 读书笔记之:C++ Primer (第4版)及习题(ch01-ch11)
- C语言实现双向非循环链表(带头结点尾结点)的基本操作
- 贪吃蛇游戏的C语言实现
- c++知识点
- C++构造函数
- Microsoft Visual C++ 和 Borland C++ Builder 之比较
- 玩具侠-C语言学习之函数指针(2)
- C语言深度剖析-----内存管理的艺术
- [C语言][LeetCode][21]Merge Two Sorted Lists
- c++ socket 客户端库 socks5 客户端 RudeSocket™ Open Source C++ Socket Library
- 三种工厂模式的分析以及C++实现
- C++栈(stack)
- [C++11]_[初级]_[weak_ptr的使用场景]
- [C++11]_[初级]_[weak_ptr的使用场景]
- C/C++程序员必须熟练应用的开源项目
- C++封装继承多态总结