Refactoring
This commit is contained in:
43
SiaDrive.Api/AutoThread.cpp
Normal file
43
SiaDrive.Api/AutoThread.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "stdafx.h"
|
||||
#include "AutoThread.h"
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
CAutoThread::CAutoThread(const CSiaCurl& siaCurl) :
|
||||
_siaCurl(siaCurl.GetHostConfig()),
|
||||
_stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr))
|
||||
{
|
||||
}
|
||||
|
||||
CAutoThread::~CAutoThread()
|
||||
{
|
||||
StopAutoThread();
|
||||
::CloseHandle(_stopEvent);
|
||||
}
|
||||
|
||||
void CAutoThread::StartAutoThread()
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_startStopMutex);
|
||||
if (!_thread)
|
||||
{
|
||||
_thread.reset(new std::thread([this]() {
|
||||
do
|
||||
{
|
||||
AutoThreadCallback(_siaCurl);
|
||||
} while (::WaitForSingleObject(_stopEvent, 2000) == WAIT_TIMEOUT);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
void CAutoThread::StopAutoThread()
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_startStopMutex);
|
||||
if (_thread)
|
||||
{
|
||||
::SetEvent(_stopEvent);
|
||||
_thread->join();
|
||||
_thread.reset(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user