From c06402155ea8c599415f60647233245cbe5d1735 Mon Sep 17 00:00:00 2001 From: moe li Date: Mon, 25 May 2026 12:01:12 +0800 Subject: [PATCH] blocking wait function for process exit --- src/raylib.h | 1 + src/rcore.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index 2d585e487..51d886321 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1260,6 +1260,7 @@ RLAPI int GetTouchPointCount(void); // Get number of t // Process execution functions RLAPI Process InitProcess(const char *command, char *const args[]); // Initialize a new process, returns a Process struct RLAPI ProcessInfo CheckProcess(Process process); // Check if a process is still running +RLAPI int WaitProcess(Process process); // Wait for process to finish, returns exit code, blocking RLAPI void PauseProcess(Process process); // Pause process execution RLAPI void ResumeProcess(Process process); // Resume paused process execution RLAPI void CloseProcess(Process process); // Close process and free resources diff --git a/src/rcore.c b/src/rcore.c index 95c758d62..b1dbb4611 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -4428,6 +4428,65 @@ RLAPI ProcessInfo CheckProcess(Process process) return info; } +RLAPI int WaitProcess(Process process) +{ + if (process.pid <= 0) + { + return -1; + } + +#if defined(_WIN32) + // PROCESS_QUERY_INFORMATION | SYNCHRONIZE + // SYNCHRONIZE for WaitForSingleObject + void *hProcess = OpenProcess(0x0400 | 0x00100000, 0, (unsigned long)process.pid); + if (hProcess == NULL) + { + TRACELOG(LOG_WARNING, "PROCESS: Failed to open process handle"); + return -1; + } + + // Wait indefinitely for process termination + // INFINITE + WaitForSingleObject(hProcess, 0xFFFFFFFF); + + // Get exit code + unsigned long exitCode = 0; + if (GetExitCodeProcess(hProcess, &exitCode)) + { + CloseHandle(hProcess); + return (int)exitCode; + } + + CloseHandle(hProcess); + return -1; +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__) + int status = 0; + pid_t result = waitpid(process.pid, &status, 0); + + if (result == process.pid) + { + if (WIFEXITED(status)) + { + // Normal exit + return WEXITSTATUS(status); + } + else if (WIFSIGNALED(status)) + { + // Terminated by signal + return -1; + } + } + else + { + TRACELOG(LOG_WARNING, "PROCESS: Failed to wait for process"); + return -1; + } +#else + TRACELOG(LOG_WARNING, "PROCESS: Process management not supported on this platform"); + return -1; +#endif +} + // Pause process execution RLAPI void PauseProcess(Process process) {