diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-22 01:02:34 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-22 01:02:34 +0200 |
commit | 28b87e4d9c60b49d46b03a19b7b83e23e222087a (patch) | |
tree | 1f0a71ca09f04eac51fdabf2e8cfabc87a6e02a3 /c/main.cpp | |
parent | 801c10f07d1c92e1b1b89d8a30cb0cf86745de31 (diff) | |
download | EpisodeBrowser-28b87e4d9c60b49d46b03a19b7b83e23e222087a.tar.gz |
Handle exceptions in fetching thread.
Diffstat (limited to 'c/main.cpp')
-rw-r--r-- | c/main.cpp | 35 |
1 files changed, 6 insertions, 29 deletions
@@ -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<wchar_t*>(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(); |