等待进程终止WaitForSingleObject()

有时需要启动一个程序并等待其终止后,主程序再继续执行。

可用WaitForSingleObject()函数实现

DWORD WINAPI WaitForSingleObject(
  __in          HANDLE hHandle,
  __in          DWORD dwMilliseconds
);


示例

 

#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; }

你可能感兴趣的:(等待进程终止WaitForSingleObject())