-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKRunnable.cpp
218 lines (192 loc) · 3.83 KB
/
KRunnable.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "KRunnable.h"
#include <thread>
#include <iostream>
static ATOM WindowClass;
HWND hwnd;
static LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
if (msg == ON_K_EVENT) {
//prevent all exceptions.
KRunnable* kr = ((KRunnable*)lp);
__try {
kr->run();
kr->End();
}
__except (1) {
kr->Fail();
}
//also,no exception show be here.
__try {
if(!kr->is_timer)
delete kr;
}
__except (1) {}
return 0;
}
return DefWindowProc(hwnd, msg, wp, lp);
}
void createMessageWindow() {
HINSTANCE hinst = ::GetModuleHandle(NULL);
if (!WindowClass) {
//make a window class
WNDCLASSEXW wcex = {
/*size*/sizeof(WNDCLASSEX), /*style*/0, /*proc*/WndProc, /*extra*/0L,0L, /*hinst*/hinst,
/*icon*/NULL, /*cursor*/NULL, /*brush*/NULL, /*menu*/NULL,
/*class*/L"KHttpServer Msg wnd class", /*smicon*/NULL };
while (!WindowClass)
WindowClass = ::RegisterClassExW(&wcex);
}
//create a window for this instance to receive messahes
hwnd = ::CreateWindowExW(0, (LPCWSTR)MAKELONG(WindowClass, 0), L"KRunnableMsg",
0, 0, 0, 1, 1, HWND_MESSAGE, NULL, hinst, NULL);
}
KRunnable::KRunnable(Runnable func) :run(func),future(NULL) {}
KRunnable::KRunnable(Runnable func, KFuture* future) : run(func), future(future)
{}
KRunnable* KRunnable::runTask()
{
if (!is_running) {
Start();
if (!hwnd)
createMessageWindow();
if (!PostMessageW(hwnd, ON_K_EVENT, (WPARAM)this, (LPARAM)this))
std::cout<<GetLastError();
}
return this;
}
KRunnable* KRunnable::runTaskLater(long time)
{
if (!is_running) {
Start();
std::thread th([this, time] {
std::this_thread::sleep_for(std::chrono::milliseconds(time));
runTask();
End();
if (!is_timer)
delete this;
});
th.detach();
}
return this;
}
KRunnable* KRunnable::runTaskAsync()
{
Start();
std::thread th([this] {
try {
this->run();
End();
}
catch(...){
}
if (!is_timer)
delete this;
//delete th;
});
th.detach();
return this;
}
KRunnable* KRunnable::runTaskLaterAsync(long time)
{
Start();
std::thread th([this, time] {
std::this_thread::sleep_for(std::chrono::milliseconds(time));
run();
End();
if (!is_timer)
delete this;
});
th.detach();
return this;
}
KRunnable* KRunnable::runTaskTimer(long first, long interval)
{
is_timer = true;
Start();
std::thread th([this,first,interval] {
std::this_thread::sleep_for(std::chrono::milliseconds(first));
while (is_timer) {
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
if (is_timer)
runTask();
}
End();
});
th.detach();
return this;
}
KRunnable* KRunnable::runTaskTimerAsync(long first, long interval)
{
is_timer = true;
Start();
std::thread th([this, first, interval] {
std::this_thread::sleep_for(std::chrono::milliseconds(first));
while (is_timer) {
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
if (is_timer)
runTaskAsync();
}
End();
});
th.detach();
return this;
}
KRunnable* KRunnable::cancelTimer()
{
is_timer = false;
return this;
}
bool KRunnable::isRunning()
{
return is_running;
}
bool KRunnable::setFuture(KFuture* future) {
if (this->future == NULL || this->future->isCompleted()) {
delete this->future;
this->future = future;
return true;
}
else
return false;
}
KFuture* KRunnable::getFuture() {
if (future)
return future;
else
return (future = new KFuture());
}
KFuture::KFuture()
{
}
void KFuture::start()
{
waitlock.lock();
}
void KFuture::complete()
{
waitlock.unlock();
}
bool KFuture::wait(long time)
{
bool res= waitlock.try_lock_for(std::chrono::milliseconds(time));
if (res)
waitlock.unlock();
return res;
}
void KFuture::wait()
{
waitlock.lock();
waitlock.unlock();
}
bool KFuture::isCompleted()
{
if (waitlock.try_lock()) {
waitlock.unlock();
return true;
}
else
return false;
}
void KFuture::fail()
{
waitlock.unlock();
}