-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThreadBase.cpp
More file actions
100 lines (82 loc) · 1.56 KB
/
ThreadBase.cpp
File metadata and controls
100 lines (82 loc) · 1.56 KB
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
#include "stdafx.h"
#include "ThreadBase.h"
struct _THREAD_PARAM
{
void* pThis;
HANDLE hStepEvent;
};
CTheardBase::CTheardBase() : m_hThread(NULL), m_hStopEvent(NULL)
{
}
CTheardBase::~CTheardBase()
{
Stop();
}
BOOL CTheardBase::Start()
{
if (m_hThread)
{
CloseHandle(m_hThread);
m_hThread = NULL;
}
if (m_hStopEvent)
{
::SetEvent(m_hStopEvent);
m_hStopEvent = NULL;
}
if (!(m_hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
return FALSE;
_THREAD_PARAM* pParam = new _THREAD_PARAM;
if (! pParam)
return FALSE;
pParam->pThis = this;
pParam->hStepEvent = m_hStopEvent;
if (!(m_hThread = (HANDLE)_beginthreadex( NULL, 0, CTheardBase::ThreadFunc, pParam, 0, NULL)))
return FALSE;
return TRUE;
}
BOOL CTheardBase::Stop()
{
if (m_hStopEvent)
{
SetEvent(m_hStopEvent);
m_hStopEvent = NULL;
}
if (m_hThread)
{
WaitForSingleObject( m_hThread, 100000 );
CloseHandle(m_hThread);
m_hThread = NULL;
}
return TRUE;
}
BOOL CTheardBase::IsRun()
{
return m_hThread && WaitForSingleObject(m_hThread, 0) == WAIT_TIMEOUT;
}
unsigned int WINAPI CTheardBase::ThreadFunc(LPVOID lpParameter)
{
_THREAD_PARAM* pParam = (_THREAD_PARAM*)lpParameter;
if (! pParam || !pParam->pThis)
return 0;
CTheardBase* pThis = (CTheardBase*)pParam->pThis;
HANDLE hHandle = pParam->hStepEvent;
delete pParam;
pParam = NULL;
if (pThis)
{
::CoInitialize(NULL);
pThis->OnThreadFunc(hHandle);
::CoUninitialize();
}
if (hHandle)
{
CloseHandle(hHandle);
hHandle = NULL;
}
return 0;
}
void CTheardBase::OnThreadFunc(HANDLE hStopEvent)
{
return;
}