forked from Appendko/NicoCommentPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleThread.h
88 lines (75 loc) · 2.7 KB
/
SimpleThread.h
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
/********************************************************************************
Copyright (C) 2014 Append Huang <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
********************************************************************************/
#include <Windows.h>
#pragma once
class SimpleThread{
protected:
HANDLE thread;
HANDLE timer;
HANDLE stopReq;
bool bKillThread;
virtual DWORD Run() {return 0;}
static DWORD WINAPI ThreadProc(LPVOID pParam){ return static_cast<SimpleThread*>(pParam)->Run(); }
public:
inline bool isAlive() {return (thread!=0);}
SimpleThread() {
thread = 0;
bKillThread = true;
stopReq = CreateEvent(NULL, FALSE, FALSE, NULL);
}
~SimpleThread() {
StopThread();
CloseHandle(stopReq);
}
void StartThread() {
onSysMsg(L"StartThread(): Thread %d",(int)thread);
if(thread==0) {
//onDebugMsg(L"Begin StartThread(): Thread %d",(int)thread);
bKillThread = false;
thread = CreateThread(NULL, 0, ThreadProc, (LPVOID)this, 0, NULL);
}
}
void StopThread () {
bKillThread = true;
onSysMsg(L"StopThread(): Thread %d",(int)thread);
if(thread!=0) {
//onDebugMsg(L"Begin StopThread(): Thread %d",(int)thread);
DWORD retv;
retv=WaitForSingleObject(thread,30000);
switch(retv){
case WAIT_TIMEOUT://Failed waiting//Extremely Case
case WAIT_ABANDONED:
if(retv==WAIT_TIMEOUT) onDebugMsg(L"Thread Closing: TIMEOUT, FORCE TERMINATING THE THREAD");
if(retv==WAIT_ABANDONED) onDebugMsg(L"Thread Closing: ABANDONED, FORCE TERMINATING THE THREAD");
onDebugMsg(L"Thread Closing: Might Lead to Some UNSTABLITY");
DWORD exitcode;
GetExitCodeThread(thread,&exitcode);
TerminateThread(thread,exitcode);
break;
case WAIT_FAILED:
if(thread==0) onSysMsg(L"Thread Closing: Already Closed, thread=%d",thread);
else onDebugMsg(L"Thread Closing: FAILED, thread=%d",thread);
break;
case WAIT_OBJECT_0:
onSysMsg(L"Thread Closing: Finished");
}
onSysMsg(L"Thread Closed: RETV = 0x%08X",retv);
if(thread!=0) {
CloseHandle(thread);
thread = 0;
}
}
}
};