blocking wait function for process exit

This commit is contained in:
moe li 2026-05-25 12:01:12 +08:00
parent c236b4eaaf
commit c06402155e
2 changed files with 60 additions and 0 deletions

View File

@ -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

View File

@ -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)
{