#include <windows.h>
#include <iostream>
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()
{
int timer1 = 1;
HWND hwndTimer;
MSG msg;
SetTimer(NULL,timer1,5000,TimerProc); // // 窗口句柄 、定时器ID,多个定时器时,可以通过该ID判断是哪个定时器
int temp = 0;
while ( temp = GetMessage(&msg, NULL,NULL,NULL)!= 0 && temp != -1)
{
if (msg.message == WM_TIMER)
{
std::cout << "i got the message" << std::endl;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
#include <iostream>
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()
{
int timer1 = 1;
HWND hwndTimer;
MSG msg;
SetTimer(NULL,timer1,5000,TimerProc); // // 窗口句柄 、定时器ID,多个定时器时,可以通过该ID判断是哪个定时器
int temp = 0;
while ( temp = GetMessage(&msg, NULL,NULL,NULL)!= 0 && temp != -1)
{
if (msg.message == WM_TIMER)
{
std::cout << "i got the message" << std::endl;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
/非常好的一个线程执行一个定时器
[1].[代码] h 跳至 [1] [2] [3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#pragma once
#include <process.h>
#include <Windows.h>
typedef
void
(*TimeProc)(
WPARAM
wParam,
LPARAM
wLParam);
enum
TIMER_STATE
{
ORIGINAL = 1,
STOP,
PAUSE,
RUN,
RESUME
};
class
CMyTimer
{
public
:
CMyTimer(unsigned
long
dwSecond, TimeProc lptFun);
~CMyTimer(
void
);
bool
Start();
bool
Stop();
void
SetTimerParam(unsigned
long
dwSecond, TimeProc lptFun);
private
:
TIMER_STATE eState;
unsigned
long
dwSec;
unsigned
long
dwThreadId;
int
nEvenId;
TimeProc lpFun;
private
:
static
void
TimerThread(
void
* arg);
};
|
[2].[代码] cpp 跳至 [1] [2] [3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#include "StdAfx.h"
#include "MyTimer.h"
CMyTimer::CMyTimer(unsigned
long
dwSecond, TimeProc lptFun):dwSec(dwSecond), lpFun(lptFun)
{
eState = ORIGINAL;
nEvenId = 0;
//lpFun = NULL;
}
CMyTimer::~CMyTimer(
void
)
{
if
(
this
->nEvenId > 0)
{
KillTimer(NULL,
this
->nEvenId);
}
}
bool
CMyTimer::Start()
{
if
(lpFun == NULL || dwSec <= 0)
{
return
false
;
}
this
->dwThreadId = _beginthread(
this
->TimerThread, 0, (
void
*)
this
);
return
true
;
}
void
CMyTimer::TimerThread(
void
* arg)
{
CMyTimer * pTmep = (CMyTimer *)arg;
pTmep->nEvenId = SetTimer(NULL, 0 , pTmep->dwSec, NULL);
if
(pTmep->nEvenId > 0)
{
pTmep->eState = RUN;
}
MSG msg;
while
(GetMessage(&msg,NULL, 0, 0))
{
if
(pTmep->eState == STOP)
{
pTmep->eState = ORIGINAL;
KillTimer(NULL, pTmep->nEvenId);
pTmep->nEvenId = 0;
pTmep->dwThreadId = 0;
break
;
}
if
(msg.message==WM_TIMER)
{
pTmep->lpFun(msg.
time
, NULL);
}
}
_endthread();
}
void
CMyTimer::SetTimerParam(unsigned
long
dwSecond, TimeProc lptFun)
{
if
(dwSec <= 0 || lpFun == NULL)
{
return
;
}
dwSec = dwSecond;
lpFun = lptFun;
}
bool
CMyTimer::Stop()
{
eState = STOP;
return
true
;
}
|
[3].[代码] 测试文件 跳至 [1] [2] [3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
// timer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "MyTimer.h"
//回调函数
void
timer1(
WPARAM
wParam,
LPARAM
wLParam)
{
printf
(
"timer1 is run %d\n"
, wParam);
}
void
timer2(
WPARAM
wParam,
LPARAM
wLParam)
{
printf
(
"timer2 is run %d\n"
, wParam);
}
int
main()
{
//timer 1
CMyTimer a(1000, timer1);
a.Start();
//timer 2
CMyTimer b(2000, timer2);
b.Start();
getchar
();
a.Stop();
getchar
();
a.Start();
getchar
();
return
0;
}
|