diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2018-08-21 22:47:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-21 22:47:21 +0200 |
commit | 4bf15d809013c6c70af20632f181b5ca0e75e47c (patch) | |
tree | 51502d749dac35cd39468577bc77a241a6aad5cf | |
parent | 0a8ac1a57c00db32464b7ea30dd9fd6404936948 (diff) | |
parent | ca1c851dbdc14701695d9589225b44d5681606f0 (diff) |
Merge pull request #21236 from dragmz/windows-graceful-close
Try closing gracefully before terminating process
-rw-r--r-- | core/os/os.h | 2 | ||||
-rw-r--r-- | drivers/unix/os_unix.cpp | 2 | ||||
-rw-r--r-- | drivers/unix/os_unix.h | 2 | ||||
-rw-r--r-- | editor/editor_run.cpp | 4 | ||||
-rw-r--r-- | platform/windows/os_windows.cpp | 38 | ||||
-rw-r--r-- | platform/windows/os_windows.h | 2 |
6 files changed, 39 insertions, 11 deletions
diff --git a/core/os/os.h b/core/os/os.h index dd783408e8..12c0222ad4 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -254,7 +254,7 @@ public: virtual String get_executable_path() const; virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false) = 0; - virtual Error kill(const ProcessID &p_pid) = 0; + virtual Error kill(const ProcessID &p_pid, const int p_max_wait_msec = -1) = 0; virtual int get_process_id() const; virtual Error shell_open(String p_uri); diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 05dfd69f58..8aab4cb521 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -329,7 +329,7 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo return OK; } -Error OS_Unix::kill(const ProcessID &p_pid) { +Error OS_Unix::kill(const ProcessID &p_pid, const int p_max_wait_msec) { int ret = ::kill(p_pid, SIGKILL); if (!ret) { diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index 95b74d23ff..c5240231fa 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -91,7 +91,7 @@ public: virtual uint64_t get_ticks_usec() const; virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false); - virtual Error kill(const ProcessID &p_pid); + virtual Error kill(const ProcessID &p_pid, const int p_max_wait_msec = -1); virtual int get_process_id() const; virtual bool has_environment(const String &p_var) const; diff --git a/editor/editor_run.cpp b/editor/editor_run.cpp index 749cf6aa2b..072bd948e1 100644 --- a/editor/editor_run.cpp +++ b/editor/editor_run.cpp @@ -181,8 +181,8 @@ Error EditorRun::run(const String &p_scene, const String p_custom_args, const Li void EditorRun::stop() { if (status != STATUS_STOP && pid != 0) { - - OS::get_singleton()->kill(pid); + const int max_wait_msec = GLOBAL_DEF("editor/stop_max_wait_msec", 10000); + OS::get_singleton()->kill(pid, max_wait_msec); } status = STATUS_STOP; diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index d5bb85c035..ca3bbedef2 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -190,6 +190,28 @@ BOOL WINAPI HandlerRoutine(_In_ DWORD dwCtrlType) { } } +BOOL CALLBACK _CloseWindowsEnum(HWND hWnd, LPARAM lParam) { + DWORD dwID; + + GetWindowThreadProcessId(hWnd, &dwID); + + if (dwID == (DWORD)lParam) { + PostMessage(hWnd, WM_CLOSE, 0, 0); + } + + return TRUE; +} + +bool _close_gracefully(const PROCESS_INFORMATION &pi, const DWORD dwStopWaitMsec) { + if (!EnumWindows(_CloseWindowsEnum, pi.dwProcessId)) + return false; + + if (WaitForSingleObject(pi.hProcess, dwStopWaitMsec) != WAIT_OBJECT_0) + return false; + + return true; +} + void OS_Windows::initialize_debugging() { SetConsoleCtrlHandler(HandlerRoutine, TRUE); @@ -2322,20 +2344,26 @@ Error OS_Windows::execute(const String &p_path, const List<String> &p_arguments, return OK; }; -Error OS_Windows::kill(const ProcessID &p_pid) { - +Error OS_Windows::kill(const ProcessID &p_pid, const int p_max_wait_msec) { ERR_FAIL_COND_V(!process_map->has(p_pid), FAILED); const PROCESS_INFORMATION pi = (*process_map)[p_pid].pi; process_map->erase(p_pid); - const int ret = TerminateProcess(pi.hProcess, 0); + Error result; + + if (p_max_wait_msec != -1 && _close_gracefully(pi, p_max_wait_msec)) { + result = OK; + } else { + const int ret = TerminateProcess(pi.hProcess, 0); + result = ret != 0 ? OK : FAILED; + } CloseHandle(pi.hProcess); CloseHandle(pi.hThread); - return ret != 0 ? OK : FAILED; -}; + return result; +} int OS_Windows::get_process_id() const { return _getpid(); diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index 69c7d851b8..8e39e4c990 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -258,7 +258,7 @@ public: virtual uint64_t get_ticks_usec() const; virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false); - virtual Error kill(const ProcessID &p_pid); + virtual Error kill(const ProcessID &p_pid, const int p_stop_max_wait_msec = -1); virtual int get_process_id() const; virtual bool has_environment(const String &p_var) const; |