From 28b87e4d9c60b49d46b03a19b7b83e23e222087a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 22 Aug 2022 01:02:34 +0200 Subject: Handle exceptions in fetching thread. --- c/main.cpp | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) (limited to 'c/main.cpp') diff --git a/c/main.cpp b/c/main.cpp index d6e3027..73a89d4 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -24,7 +24,7 @@ /* Exit gracefully on uncaught exception. */ static void OnTerminate() noexcept; -static auto unused = std::set_terminate(OnTerminate); +static auto UNUSED = std::set_terminate(OnTerminate); /* main.cpp defines all global (non-template) variables used in the * program. `extern' is used to access them from other files, when @@ -81,29 +81,7 @@ static void UpdateTheme(); void OnTerminate() noexcept { - const wchar_t* what = L"an exception"; - WcharPtr why; - - try { - std::rethrow_exception(std::current_exception()); - } catch (const term_t& t) { - what = L"a Prolog exception"; - try { why = PlString(t); } catch (...) {} - } catch (const Win32Error& e) { - what = L"a Windows error"; - try { why = WcharPtr::Copy(e.What()); } catch (...) {} - } catch (const std::exception& e) { - try { why = WcharPtr::FromNarrow(e.what()); } catch (...) {} - } catch (...) {} - - wchar_t msg[256] = {0}; - if (why) - Swprintf(msg, L"Episode Browser was terminated due to %s: %s", - what, static_cast(why)); - else - Swprintf(msg, L"Episode Browser was terminated due to %s.", what); - - MessageBox(g_hWnd, msg, L"Fatal Error", MB_ICONERROR); + ShowException(L"Episode Browser was terminated due to %s: %s", L"Fatal Error", MB_ICONERROR); _Exit(1); } @@ -402,7 +380,7 @@ void HandleMainMenu(const HWND hWnd, const WORD command) case IDM_FILE_FETCH_DATA: { - WaitFor(FetchData); + WaitFor(WaitFetchData); break; } @@ -456,13 +434,11 @@ void HandleMainMenu(const HWND hWnd, const WORD command) void WaitFor(void (*f)(bool*)) { - /* WaitFor uses a thread on the Prolog side to execute a - * predicate asynchronously. */ - static bool bActive = false; static bool bDone = false; static UINT_PTR iTimer; + /* Ensure that only a single thread is waited on. */ if (bActive) { if (EBMessageBox(L"Another task is active. " L"Do you want to cancel the existing task and start a new one?", @@ -476,7 +452,6 @@ void WaitFor(void (*f)(bool*)) /* The timer procedure animates an ellipsis in the status bar * while the thread is running. */ - static auto proc = [](HWND, UINT, UINT_PTR, DWORD) -> void { static int i = 0; @@ -494,6 +469,8 @@ void WaitFor(void (*f)(bool*)) } }; + /* The waited-on function signals its completion by setting a + * shared boolean value to true. */ bDone = false; bActive = true; std::thread{f, &bDone}.detach(); -- cgit v1.2.3