LWQ
2025-08-13 be72a344954f46a40c96848071a10996fc4bf030
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
 
#include "StdAfx.h"
#include "ThreadControl.h"
 
CThreadControl::CThreadControl(void)
{
    m_nWorkingThread= 0;
    m_nThreadLoop= 0;
    m_pThread= NULL;
    m_pThreadWorker= NULL;
    InitializeCriticalSection(&m_csWorkingEnd);
}
 
CThreadControl::~CThreadControl(void)
{
    ReleaseThreadControl();
    DeleteCriticalSection(&m_csWorkingEnd);
}
 
int    CThreadControl::StartThreadControl()
{
    if (m_pThread)
    {
        for (int i = 0; i < m_nThreadLoop; i++)
        {
            m_pThread[i].StartThreadWork();
        }
    }
 
    return m_nThreadLoop;
}
 
void CThreadControl::StopThreadControl()
{
    if (m_pThread)
    {
        for (int i = 0; i < m_nThreadLoop; i++)
        {
            m_pThread[i].StopThread();
        }
    }
 
}
BOOL CThreadControl::InitThreadControl(int nThread, IThreadWorker* pThreadWorker)
{
    if(nThread == m_nThreadLoop)
        return TRUE;
 
    ReleaseThreadControl();
 
    if(nThread < 1)
        nThread= 1;
    if(nThread > MAX_THREAD)
        nThread= MAX_THREAD;
 
    m_pThreadWorker = pThreadWorker;
    m_pThread = new CInspectThread[nThread];
 
    m_nThreadLoop = 0;
    for (int i = 0; i < nThread; i++)
    {
        m_pThread[i].SetWorker(static_cast<IThreadWorker*>(this));
        m_pThread[i].StartThreadLoop(i);
        m_nThreadLoop++;
    }
 
    return TRUE;
}
 
BOOL CThreadControl::ReleaseThreadControl()
{
    if (m_pThread && m_nThreadLoop > 0)
    {
        int i;
        for (i = 0; i < m_nThreadLoop; i++)
        {
            m_pThread[i].StopThread();
            m_pThread[i].WaitThreadEnd();
        }
 
        Sleep(500);
 
        for (i = 0; i < m_nThreadLoop; i++)
        {
            if (m_pThread[i].IsRunning())
            {
                m_pThread[i].SuspendThread();
                Sleep(10);
                TerminateThread(m_pThread[i].m_hThread, 0);
                g_pLog->DisplayMessage(_T("Deinit :: Terminate Thread : %d"), i);
            }
        }
 
        m_nThreadLoop = 0;
 
        delete [] m_pThread;
        m_pThread = NULL;
    }
 
    return TRUE;
}
 
BOOL CThreadControl::OnThreadRun(int iThread, CInspectThread *pInspectThread)
{
    EnterCriticalSection(&m_csWorkingEnd);
    m_nWorkingThread++;
    LeaveCriticalSection(&m_csWorkingEnd);
 
    Sleep(1);// 窍绰 老捞 绝栏搁  OnThreadEndAll 捞 静饭靛 俺荐 父怒 龋免瞪 荐 乐促.
 
    if(m_pThreadWorker)
        return m_pThreadWorker->OnThreadRun(iThread, pInspectThread);
    return TRUE;
}
BOOL CThreadControl::OnThreadEnd(int iThread, CInspectThread *pInspectThread)
{
    BOOL bRet= FALSE;
 
    if(m_pThreadWorker)
        m_pThreadWorker->OnThreadEnd(iThread, pInspectThread);
    EnterCriticalSection(&m_csWorkingEnd);
    m_nWorkingThread--;
    if(m_nWorkingThread == 0)
    {
        bRet= OnThreadEndAll();
    }
    LeaveCriticalSection(&m_csWorkingEnd);
    return bRet;//m_nWorkingThread == 0;
}
BOOL CThreadControl::OnThreadEndAll()
{
    BOOL bRet= TRUE;
    if(m_pThreadWorker)
    {
        bRet= m_pThreadWorker->OnThreadEndAll();
    }
    return bRet;
}
 
 
IMPLEMENT_DYNCREATE(CInspectThread, CWinThread)
CInspectThread::~CInspectThread()
{
    DeleteCriticalSection(&m_csExit);
}
CInspectThread::CInspectThread()
{
    m_bRunning        = FALSE;
    m_bThreadRunning    = FALSE;
    m_bWorking            = FALSE;
    m_iThread            = -1;
    m_iCamera            = -1;
 
//    m_nFrameDefBlobLimit    = 0;
 
    m_pWorker= NULL;
    InitializeCriticalSection(&m_csExit);
 
}
 
BEGIN_MESSAGE_MAP(CInspectThread, CWinThread)
    //{{AFX_MSG_MAP(CInspectionThread)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
BOOL CInspectThread::StartThreadLoop(int iThread)
{
    if (m_bRunning)
        return FALSE;
 
    m_iThread = iThread;
    EnterCriticalSection(&m_csExit);
    m_bRunning = TRUE;
    LeaveCriticalSection(&m_csExit);
    CreateThread();
 
    return TRUE;
}
 
void CInspectThread::StopThread()
{
    EnterCriticalSection(&m_csExit);
    m_bRunning = FALSE;
    LeaveCriticalSection(&m_csExit);
}
BOOL CInspectThread::WaitThreadEnd(int ms)
{
    int count= 0;
    while (m_bThreadRunning)
    {
        Sleep(1);
        count++;
        if(ms > 0)    // 措扁矫埃 力茄捞 乐栏搁.
        {
            if(count > ms)
                return FALSE; // 措扁矫埃 悼救 沥惑 辆丰 给窍绊 府畔.
        }
    }
    return TRUE;    // 沥惑 辆丰 犬牢.
}
 
BOOL CInspectThread::InitInstance()
{
    // TODO:  perform and per-thread initialization here
    return TRUE;
}
 
int CInspectThread::ExitInstance()
{
    // TODO:  perform any per-thread cleanup here
    return CWinThread::ExitInstance();
}
 
int CInspectThread::Run()
{
    m_bThreadRunning= TRUE;
 
    while(m_bRunning && m_pWorker)
    {
        if(m_bWorking)
        {
            m_pWorker->OnThreadRun(m_iThread, this);
            m_pWorker->OnThreadEnd(m_iThread, this);
            m_bWorking= FALSE;
        }else
        {
            Sleep(1);
        }
    }    
    ExitInstance();
    m_bThreadRunning= FALSE;
    return CWinThread::Run();
}