C++ 中定时器的用法
作者:网络转载 发布时间:[ 2013/4/1 10:16:59 ] 推荐标签:
3、取消定时器
不再使用定时器后,我们应该调用KillTimer来取消定时,KillTimer的原型如下
BOOL KillTimer(
HWND hWnd, // 窗口句柄
UINT_PTR uIDEvent // ID
);
在MFC程序中我们可以直接调用KillTimer(int nIDEvent)来取消定时器。
例子
#include
#include
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
std::cout < < "hello " < < std::endl;
}
void main()
SetTimer(NULL,timer1,5000,TimerProc);
输出如下:
i got the message
// timer.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
unsigned long WINAPI Thread(PVOID pvoid);
unsigned long WINAPI Thread(PVOID pvoid)
{
int timer1 = 1;
HWND hwndTimer;
MSG msg;
int itemp;
while ( (itemp = GetMessage(&msg, NULL,NULL,NULL))&& (itemp!=0) && (-1 != itemp))
{
if (msg.message == WM_TIMER)
{
std::cout < < "i got the message " < < std::endl;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
hello
i got the message
hello
i got the message
hello
---------------------------------------------------------------------------------------------------------------------------
//
#include
#include
#include
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED ");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0 ");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT ");
break;
}
CloseHandle(hThread);
_getch();
}
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d ",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here ");
return 0;
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11