WebSocket_CPP_98/CC_SDK_VS/CCThread.cpp
2025-12-29 09:55:17 +08:00

89 lines
1.4 KiB
C++
Executable File

#include "CCThread.h"
CTL::Thread::Thread()
{
}
// ÒÆ³ýÁËÒÆ¶¯¹¹Ô캯Êý
// CTL::Thread::Thread(Thread&& other)
// {
// ...
// }
CTL::Thread::~Thread()
{
}
DWORD CTL::Thread::GetThreadId() const
{
return this->dwThreadId;
}
void CTL::Thread::Wait()
{
Mode = 0;
hThread = CreateThread(
NULL,
0,
MyThreadFunction,
this,
0,
&dwThreadId
);
if (hThread)
{
::WaitForSingleObject(hThread, INFINITE);
}
while (!this->IsStop)
{
Thread::SleepMS(50);
}
}
void CTL::Thread::Start()
{
Mode = 1;
hThread = CreateThread(
NULL,
0,
MyThreadFunction,
this,
0,
&dwThreadId
);
}
void CTL::Thread::StartDetch()
{
Start();
Mode = 2;
CloseHandle(hThread);
hThread = nullptr;
}
void CTL::Thread::SleepMS(int ms)
{
::Sleep(ms);
}
DWORD __stdcall CTL::Thread::MyThreadFunction(LPVOID lpParam)
{
Thread* pThread = (Thread*)lpParam;
if (pThread && pThread->task_Fun)
{
pThread->IsStop = false;
try
{
pThread->task_Fun();
}
catch (const std::exception&)
{
}
if (pThread->hThread && (pThread->Mode == 0 || pThread->Mode == 1))
{
CloseHandle(pThread->hThread);
}
pThread->IsStop = true;
}
return 0;
}