forked from floaterxk/DatDefragAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.cpp
87 lines (71 loc) · 1.15 KB
/
Thread.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
#include "StdAfx.h"
#include "Thread.h"
CThread::CThread()
{
m_hThread = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE)&ThreadProc,
(LPVOID)this,
CREATE_SUSPENDED,
&m_dwThreadId);
m_hQuit = CreateEvent(NULL, TRUE, FALSE, NULL);
}
CThread::~CThread()
{
Terminate();
}
void CThread::Terminate()
{
if (m_hThread)
{
if (m_hQuit)
{
SetEvent(m_hQuit);
if (WAIT_TIMEOUT == WaitForSingleObject(m_hThread, 3000))
TerminateThread(m_hThread, 0);
CloseHandle(m_hQuit);
m_hQuit = 0;
}
else
TerminateThread(m_hThread, 0);
CloseHandle(m_hThread);
m_hThread = 0;
}
else if (m_hQuit)
{
CloseHandle(m_hQuit);
m_hQuit = 0;
}
}
void CThread::Run()
{
ResumeThread(m_hThread);
}
BOOL CThread::Signaled()
{
if (WAIT_OBJECT_0 == WaitForSingleObject(m_hThread, 0))
return TRUE;
else
return FALSE;
}
BOOL CThread::ShouldQuit()
{
if (WAIT_OBJECT_0 == WaitForSingleObject(m_hQuit, 0))
return TRUE;
else
return FALSE;
}
DWORD WINAPI CThread::ThreadProc(CThread* This)
{
BOOL Result = This->Work();
#if _DEBUG
if (!Result)
OutputLastError();
#endif
return (DWORD)Result;
}
BOOL CThread::Work()
{
return TRUE;
}