forked from wamsoft/win32dialog
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplethread.hpp
165 lines (139 loc) · 4.66 KB
/
simplethread.hpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef _simplethread_hpp_
#define _simplethread_hpp_
#include <process.h>
#include <vector>
template <class T>
class SimpleThreadBase {
public:
typedef std::vector<HANDLE> EventArray;
struct ThreadStartParam {
SimpleThreadBase *self;
HANDLE prepare, stop;
T param;
};
SimpleThreadBase(DWORD tout)
: threadHandle(0), prepareEvent(0), stopEvent(0), threadTimeout(tout)
{}
virtual ~SimpleThreadBase() {
threadStop(threadTimeout);
closeAllEvent();
}
void threadStart(T t) {
if (!prepareEvent) prepareEvent = createEvent();
if (!stopEvent) stopEvent = createEvent(true);
threadStop(threadTimeout);
::ResetEvent(stopEvent);
ThreadStartParam param = { this, prepareEvent, stopEvent, t };
threadHandle = (HANDLE)_beginthreadex(NULL, 0, &threadFunc, ¶m, 0, NULL);
if (threadHandle) {
HANDLE handles[] = { prepareEvent, threadHandle };
::WaitForMultipleObjects(2, handles, FALSE, INFINITE);
if (::WaitForSingleObject(threadHandle, 0) == WAIT_OBJECT_0) {
::CloseHandle(threadHandle);
threadHandle = 0;
}
}
}
void threadStop(DWORD timeout = INFINITE) {
if (threadHandle) {
::SetEvent(stopEvent);
::WaitForSingleObject(threadHandle, timeout);
::CloseHandle(threadHandle);
}
threadHandle = 0;
}
protected:
HANDLE threadHandle, prepareEvent, stopEvent;
DWORD threadTimeout;
EventArray events;
virtual unsigned threadMain(HANDLE prepare, HANDLE stop, T param) = 0;
HANDLE createEvent(bool b = false) {
HANDLE ev = ::CreateEvent(NULL, b ? TRUE : FALSE, FALSE, NULL);
events.push_back(ev);
return ev;
}
void closeEvent(HANDLE ev) {
if (ev) {
::CloseHandle(ev);
events.remove(ev);
}
}
private:
static unsigned __stdcall threadFunc(void *vparam) {
ThreadStartParam *param = (ThreadStartParam*)vparam;
unsigned retval = param->self->threadMain(param->prepare, param->stop, param->param);
_endthreadex(retval);
return retval;
}
void closeAllEvent() {
for (EventArray::iterator i = events.begin(), end = events.end(); i != end; i++) {
::CloseHandle(*i);
}
events.clear();
}
};
template <class T>
class SimpleThreadWithMessageWindow : public SimpleThreadBase<T> {
public:
typedef SimpleThreadBase<T> InheritedClass;
SimpleThreadWithMessageWindow(ttstr const &cname, ttstr const &wname, DWORD tout)
: InheritedClass(tout), messageWindow(0)
{
messageWindow = createMessageWindow(cname.c_str(), wname.c_str());
}
virtual ~SimpleThreadWithMessageWindow()
{
messageWindow = destroyMessageWindow(messageWindow);
//UnregisterMessageWindowClass();
}
static void UnregisterMessageWindowClass() {
ATOM atom = MessageWindowClass();
if (atom != 0) ::UnregisterClassW((LPCWSTR)MAKELONG(atom, 0), ::GetModuleHandle(NULL));
MessageWindowClass(0, true);
}
protected:
HWND messageWindow;
virtual LRESULT onMessage(UINT msg, WPARAM wp, LPARAM lp) = 0;
void postMessage(UINT msg, WPARAM wp, LPARAM lp) {
if (messageWindow) ::PostMessage(messageWindow, msg, wp, lp);
}
void sendMessage(UINT msg, WPARAM wp, LPARAM lp) {
if (messageWindow) ::SendMessage(messageWindow, msg, wp, lp);
}
private:
static ATOM MessageWindowClass(ATOM set = 0, bool force = false) {
static ATOM atom = 0;
if (set || force) atom = set;
return atom;
}
static LRESULT WINAPI MsgWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
if (msg >= WM_APP && msg < 0xC000) {
SimpleThreadBase *self = (SimpleThreadBase*)(::GetWindowLong(hwnd, GWL_USERDATA));
if (self) return self->onMessage(msg, wp, lp);
}
return DefWindowProc(hwnd, msg, wp, lp);
}
HWND createMessageWindow(LPCWSTR className, LPCWSTR windowName) {
HINSTANCE hinst = ::GetModuleHandle(NULL);
if (!MessageWindowClass()) {
WNDCLASSEXW wcex = {
/*size*/sizeof(WNDCLASSEX), /*style*/0, /*proc*/&MsgWndProc, /*extra*/0L,0L, /*hinst*/hinst,
/*icon*/NULL, /*cursor*/NULL, /*brush*/NULL, /*menu*/NULL, /*class*/className, /*smicon*/NULL };
if (!(MessageWindowClass(false, ::RegisterClassExW(&wcex))))
TVPThrowExceptionMessage((ttstr(TJS_W("register window class failed: "))+className).c_str());
}
HWND hwnd = ::CreateWindowExW(0, (LPCWSTR)MAKELONG(MessageWindowClass(), 0), windowName,
0, 0, 0, 1, 1, HWND_MESSAGE, NULL, hinst, NULL);
if (!hwnd) TVPThrowExceptionMessage((ttstr(TJS_W("create message window failed: "))+windowName).c_str());
::SetWindowLong(hwnd, GWL_USERDATA, (LONG)this);
return hwnd;
}
HWND destroyMessageWindow(HWND hwnd) {
if (hwnd) {
::SetWindowLong(hwnd, GWL_USERDATA, 0);
::DestroyWindow(hwnd);
}
return NULL;
}
};
#endif