Thread::Thread()
{
_Handle = NULL;
}
Thread::~Thread()
{
CloseThread();
}
void Thread::CloseThread()
{
if(_Handle)
{
CloseHandle(_Handle);
bool threadDone = false;
while(!threadDone)
{
DWORD ExitCode = 0;
BOOL Success = GetExitCodeThread(_Handle, &ExitCode);
if(!Success || ExitCode != STILL_ACTIVE)
{
threadDone = true;
}
else
{
Sleep(1);
}
}
_Handle = NULL;
}
}
void Thread::Begin(LPTHREAD_START_ROUTINE StartFunction, void *Context)
{
PersistentAssert(_Handle == NULL, "Begin called on active thread");
_Handle = CreateThread(NULL, 0, StartFunction, Context, 0, NULL);
PersistentAssert(_Handle != NULL, "CreateThread failed");
}