C++定时器
来源:互联网 发布:在淘宝上开店靠谱吗 编辑:程序博客网 时间:2024/06/07 02:29
//方式一
#include <iostream>
#include <Windows.h>using namespace std;
void CALLBACK TimeProc(HWND hwnd,UINT message,UINT idTimer,DWORD dwTime);
int main(int argc, char * argv[])
{
SetTimer(NULL,1,1000,TimeProc);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
if (msg.message == WM_TIMER)
{
DispatchMessage(&msg);
}
}
return 0;
}
int ncount = 0;
void CALLBACK TimeProc(HWND hwnd,UINT message,UINT idTimer,DWORD dwTime)
{
cout << "\r" << ncount++<<flush;
}
方式二
#include <Windows.h>
#include <iostream>
using namespace std;
int ncount = 0;
void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT_PTR idEvent,DWORD dwTime)
{
if (nMsg== WM_TIMER)
{
cout << "\r" << ncount++<<flush;
}
}
DWORD CALLBACK ThreadFun(PVOID pvoid)
{
UINT timerid = SetTimer(NULL,1,1000,TimerProc);
MSG msg;
BOOL bRet;
//阻塞等待消息到来
while ((bRet = GetMessage(&msg,NULL,0,0)) != 0)
{
if (bRet == -1)
{
cout<<"error:"<<GetLastError()<<endl;
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//KillTimer(NULL,timerid);
cout<<"thread end here!"<<endl;
return 0;
}
int main(int argc, char * argv[])
{
HANDLE hThread = CreateThread(NULL,0,ThreadFun,NULL,0,NULL);
getchar();
return 0;
}
方式三(不精准)
#include <Windows.h>
#include <iostream>
using namespace std;
int ncount=0;
DWORD WINAPI ThreadFun(LPVOID pama)
{
UINT oldTickCount = GetTickCount();
while(TRUE)
{
while(TRUE)
{
UINT newTickCount = GetTickCount();
if(newTickCount - oldTickCount >= 1000)
{
oldTickCount = newTickCount;
break;
}
}
//代码
cout << "\r" << ncount++<<flush;
}
return 0;
}
int main(int argc, char * argv[])
{
HANDLE hThread = CreateThread(NULL,0,ThreadFun,NULL,0,NULL);
getchar();
return 0;
}
- C定时器
- C 定时器
- C 定时器
- C#.net 定时器
- C#.net定时器
- C#.net定时器
- C C++ 定时器 setitime
- LINUX C 定时器
- C#.net 定时器
- linux c语言定时器
- Objective-c定时器
- linux c语言定时器
- Linux/C的定时器
- freeBSD queue.c--定时器
- linux c语言定时器
- objective-c NSTimer 定时器
- C语言实现定时器
- linux c 定时器
- java反编译javap
- [SDUT](3334)数据结构实验之栈七:出栈序列判定 ---栈
- Jmeter之JDBC Request使用方法(oracle)
- https
- Java中数组和List的转换
- C++定时器
- HDU 1062 Text Reverse
- Mysql SQL优化
- Mybatis 基础简单归纳整理
- java web 学习(1):基础概念
- 一个不知名的项目---Day07
- Vsphere笔记06 Vcenter 部署流程 1
- Java Mail发送文本邮件换行
- poj2965