From 07bde9f8575701bd0db60baf71aca2465361ff64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 21 Aug 2022 02:04:15 +0200 Subject: Reimplement WaitFor. --- c/main.cpp | 52 +++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) (limited to 'c/main.cpp') diff --git a/c/main.cpp b/c/main.cpp index 97b71b9..d6e3027 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -71,8 +72,8 @@ static void InitializeMainWindow(HWND); static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* Process main menu commands. */ static void HandleMainMenu(HWND, WORD); -/* Call Prolog predicate in other thread, if available. */ -static void WaitFor(const char*, const char*); +/* Wait for thread. */ +void WaitFor(void (*f)(bool*)); /* Handle messages to Help > About dialog. */ static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); /* Try to style application according to current Windows theme. */ @@ -400,11 +401,13 @@ void HandleMainMenu(const HWND hWnd, const WORD command) break; case IDM_FILE_FETCH_DATA: - WaitFor("episode_data","update_episode_data"); + { + WaitFor(FetchData); break; + } case IDM_FILE_FETCH_SCREENWRITERS: - WaitFor("episode_data","update_screenwriters"); + //WaitFor("episode_data","update_screenwriters"); break; case IDM_FILE_ABOUT: @@ -451,26 +454,24 @@ void HandleMainMenu(const HWND hWnd, const WORD command) } } -void WaitFor(const char* mod, const char* pred) +void WaitFor(void (*f)(bool*)) { /* WaitFor uses a thread on the Prolog side to execute a * predicate asynchronously. */ - static WcharPtr predActive; + static bool bActive = false; + static bool bDone = false; static UINT_PTR iTimer; - static atom_t aThread; - if (predActive) { - wchar_t msg[256] = {0}; - Swprintf(msg, - L"Another task (%s) is active. " + if (bActive) { + if (EBMessageBox(L"Another task is active. " L"Do you want to cancel the existing task and start a new one?", - static_cast(predActive)); - if (EBMessageBox(msg, L"Error", MB_YESNO|MB_ICONWARNING) != IDYES) + L"Error", MB_YESNO|MB_ICONWARNING) == IDYES) { + KillTimer(nullptr, iTimer); + bActive = false; + g_elv->Update(); + } else return; - KillTimer(nullptr, iTimer); - predActive = nullptr; - g_elv->Update(); } /* The timer procedure animates an ellipsis in the status bar @@ -481,22 +482,23 @@ void WaitFor(const char* mod, const char* pred) static int i = 0; static const wchar_t* text[] = {L".", L"..", L"...", L""}; - if (Pl("episode_data","thread_running",aThread)) { - i = (i+1)%(sizeof(text)/sizeof(*text)); - SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), - reinterpret_cast(text[i])); - } else { + if (bDone) { KillTimer(nullptr, iTimer); i = 0; - predActive = nullptr; + bActive = 0; g_elv->Update(); + } else { + i = (i+1)%(sizeof(text)/sizeof(*text)); + SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), + reinterpret_cast(text[i])); } }; - Plx(mod,"thread_create",pred,&aThread); + bDone = false; + bActive = true; + std::thread{f, &bDone}.detach(); SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), reinterpret_cast(L".")); - if (Prefer(iTimer = SetTimer(nullptr, -1, 500, proc))) - predActive = WcharPtr::FromNarrow(pred); + Prefer(iTimer = SetTimer(nullptr, -1, 500, proc)); } INT_PTR CALLBACK AboutDlgProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, const LPARAM) -- cgit v1.2.3