等待进程终止WaitForSingleObject()时间:2022-10-30 14:53:29有时需要启动一个程序并等待其终止后,主程序再继续执行。 可用WaitForSingleObject()函数实现 DWORD WINAPI WaitForSingleObject(<br/> __in HANDLE hHandle,<br/> __in DWORD dwMilliseconds<br/>);<br/><br/> 示例 #include "stdafx.h"#include "windows.h"int _tmain(int argc, _TCHAR* argv[]){ //LPTSTR program=_tcsdup(TEXT("notepad")); TCHAR program[MAX_PATH]=TEXT("notepad"); STARTUPINFO si; memset(&si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); /*si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_SHOW; */ PROCESS_INFORMATION pi; printf("Now starting process.../n"); CreateProcess(NULL,program,NULL,NULL,FALSE,0,NULL,NULL,&si,π); printf("Waiting.../n"); WaitForSingleObject(pi.hProcess,INFINITE); printf("OK/n"); system("pause"); return 0;}