forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessagePumpThread.cpp
73 lines (58 loc) · 1.5 KB
/
MessagePumpThread.cpp
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
68
69
70
71
72
73
#include "StdAfx.h"
#include "MessagePumpThread.h"
CMessagePumpThread::CMessagePumpThread(void)
{
}
CMessagePumpThread::~CMessagePumpThread(void)
{
}
UINT CMessagePumpThread::MessagePumpThread(void* thisptr)
{
CMessagePumpThread *threadClass = (CMessagePumpThread*)thisptr;
threadClass->RunMessagePump();
return 0;
}
void CMessagePumpThread::Start()
{
m_hEvt = CreateEvent(NULL, FALSE, FALSE, NULL);
m_thread = _beginthreadex(NULL, 0, MessagePumpThread, this, 0, &m_threadID);
if (0 == m_thread)
{
throw "Could not create thread";
}
// now wait until the thread is up and really running
if (WAIT_OBJECT_0 != WaitForSingleObject(m_hEvt, 10000L)) // 10 seconds
{
throw "Timeout waiting for thread to start";
}
}
void CMessagePumpThread::Stop()
{
PostThreadMessage(m_threadID, WM_QUIT, 0, 0L);
if (WAIT_OBJECT_0 != WaitForSingleObject(m_hEvt, 10000L))
{
throw "Timeout waiting for thread to stop";
}
};
void CMessagePumpThread::PostMsg(UINT msg, WPARAM wParam, LPARAM lParam)
{
PostThreadMessage(m_threadID, msg, wParam, lParam);
}
void CMessagePumpThread::RunMessagePump()
{
MSG msg;
// create the message queue
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
// we're far enough to let the creator know we're running
SetEvent(m_hEvt);
while(true)
{
BOOL bRet = GetMessage(&msg, NULL, 0, 0);
if (0 >= bRet) // just read the specs, it's a "MickeySoft BOOL" and can be TRUE, FALSE or -1 (right on)
{
SetEvent(m_hEvt);
break;
}
TakeMsg(msg.message, msg.wParam, msg.lParam);
}
}