diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.cpp b/support/hololens/ServoApp/ServoControl/ServoControl.cpp index d0d1e814e91c..45801fcb4255 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.cpp +++ b/support/hololens/ServoApp/ServoControl/ServoControl.cpp @@ -418,19 +418,27 @@ void ServoControl::Loop() { while (true) { EnterCriticalSection(&mGLLock); - while (mTasks.size() == 0 && !mAnimating && mLooping) { - SleepConditionVariableCS(&mGLCondVar, &mGLLock, INFINITE); - } - if (!mLooping) { + try { + while (mTasks.size() == 0 && !mAnimating && mLooping) { + SleepConditionVariableCS(&mGLCondVar, &mGLLock, INFINITE); + } + if (!mLooping) { + LeaveCriticalSection(&mGLLock); + break; + } + for (auto &&task : mTasks) { + task(); + } + mTasks.clear(); LeaveCriticalSection(&mGLLock); - break; - } - for (auto &&task : mTasks) { - task(); + mServo->PerformUpdates(); + } catch (hresult_error const &e) { + log(L"GL Thread exception: %s", e.message().c_str()); + throw e; + } catch (...) { + log(L"GL Thread exception"); + throw winrt::hresult_error(E_FAIL, L"GL Thread exception"); } - mTasks.clear(); - LeaveCriticalSection(&mGLLock); - mServo->PerformUpdates(); } mServo->DeInit(); } @@ -445,7 +453,16 @@ void ServoControl::StartRenderLoop() { } mLooping = true; log(L"BrowserPage::StartRenderLoop(). UI thread: %i", GetCurrentThreadId()); - auto task = Concurrency::create_task([=] { Loop(); }); + auto task = Concurrency::create_task([=] { + try { + Loop(); + } catch (...) { + mLooping = false; + mLoopTask.reset(); + mServo.reset(); + LeaveCriticalSection(&mGLLock); + } + }); mLoopTask = std::make_unique>(task); }