diff options
author | RadAd <adamgates84+github@gmail.com> | 2019-07-09 14:16:31 +1000 |
---|---|---|
committer | RadAd <adamgates84+github@gmail.com> | 2019-07-09 14:16:31 +1000 |
commit | 9bbb22c81d04975e8e2aaeebf98545f86d4716f0 (patch) | |
tree | 3a8e3e8e80ffc85f45d063dcf84beff8186883f6 /RadTerminal.cpp | |
parent | 8e068a499f2445266cc9f0790f6aa1e789a528b2 (diff) | |
download | RadTerminal-9bbb22c81d04975e8e2aaeebf98545f86d4716f0.tar.gz RadTerminal-9bbb22c81d04975e8e2aaeebf98545f86d4716f0.zip |
Watch for process exit in a thread
Diffstat (limited to 'RadTerminal.cpp')
-rw-r--r-- | RadTerminal.cpp | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/RadTerminal.cpp b/RadTerminal.cpp index 140b363..2993d41 100644 --- a/RadTerminal.cpp +++ b/RadTerminal.cpp @@ -181,6 +181,33 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, PTSTR pCmdLine, int nCmdSho return EXIT_SUCCESS; } +struct WatchData +{ + HANDLE h; + HWND w; +}; + +#define WM_WATCH (WM_USER + 5) + +DWORD WINAPI WatchThread(LPVOID lpParameter) +{ + const WatchData* wd = (WatchData*) lpParameter; + do + { + WaitForSingleObject(wd->h, INFINITE); + } while (SendMessage(wd->w, WM_WATCH, (WPARAM) wd->h, 0) != 0); + delete wd; + return 0; +} + +void Watch(HANDLE h, HWND w) +{ + WatchData* wd = new WatchData; + wd->h = h; + wd->w = w; + CreateThread(nullptr, 0, WatchThread, wd, 0, nullptr); +} + void tsm_log(void *data, const char *file, int line, @@ -592,6 +619,8 @@ BOOL RadTerminalWindowOnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct) return FALSE; } + Watch(data->spd.pi.hProcess, hWnd); + // TODO Report error int e = 0; e = tsm_screen_new(&data->screen, tsm_log, nullptr); @@ -946,12 +975,6 @@ void RadTerminalWindowOnTimer(HWND hWnd, UINT id) FixScrollbar(hWnd); InvalidateRect(hWnd, nullptr, TRUE); } - - DWORD exitcode = 0; - if (GetExitCodeProcess(data->spd.pi.hProcess, &exitcode) && exitcode != STILL_ACTIVE) - { - DestroyWindow(hWnd); - } } break; @@ -1109,6 +1132,17 @@ LRESULT CALLBACK RadTerminalWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR HANDLE_MSG(hWnd, WM_ACTIVATE, RadTerminalWindowOnActivate); HANDLE_MSG(hWnd, WM_VSCROLL, RadTerminalWindowOnVScroll); HANDLE_MSG(hWnd, WM_DROPFILES, RadTerminalWindowOnDropFiles); + case WM_WATCH: + { + const RadTerminalData* const data = (RadTerminalData*) GetWindowLongPtr(hWnd, GWLP_USERDATA); + HANDLE h = (HANDLE) wParam; + if (h == data->spd.pi.hProcess) + { + DestroyWindow(hWnd); + } + return 0; + } + break; //HANDLE_MSG(hWnd, WM_CHAR, RadTerminalWindowOnChar); default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } |