aboutsummaryrefslogtreecommitdiff
path: root/c/main.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-21 02:04:15 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-21 02:04:15 +0200
commit07bde9f8575701bd0db60baf71aca2465361ff64 (patch)
tree4660c1311b9ea9a14b31fa61f67f878857d80ee9 /c/main.cpp
parentb9573a485baf98e9fb749289b8b9c1f73f70d280 (diff)
downloadEpisodeBrowser-07bde9f8575701bd0db60baf71aca2465361ff64.tar.gz
Reimplement WaitFor.
Diffstat (limited to 'c/main.cpp')
-rw-r--r--c/main.cpp52
1 files changed, 27 insertions, 25 deletions
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 <exception>
#include <stdexcept>
+#include <thread>
#include <windows.h>
#include <commctrl.h>
#include <SWI-Prolog.h>
@@ -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<wchar_t*>(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<LPARAM>(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<LPARAM>(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<LPARAM>(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)