下面的代碼示例啓動另一個應用程序(本例中是 Notepad),並無限期等待該應用程序關閉。
//How to Wait for a Shelled Process to Finish //Get the path to the system folder. string sysFolder= Environment.GetFolderPath(Environment.SpecialFolder.System); //Create a new process info structure. ProcessStartInfo pInfo = new ProcessStartInfo(); //Set the file name member of the process info structure. pInfo.FileName = sysFolder + @"/eula.txt"; //Start the process. Process p = Process.Start(pInfo); //Wait for the window to finish loading. p.WaitForInputIdle(); //Wait for the process to end. p.WaitForExit(); MessageBox.Show("Code continuing...");
//Set a time-out value. int timeOut=5000; //Get path to system folder. string sysFolder= Environment.GetFolderPath(Environment.SpecialFolder.System); //Create a new process info structure. ProcessStartInfo pInfo = new ProcessStartInfo(); //Set file name to open. pInfo.FileName = sysFolder + @"/eula.txt"; //Start the process. Process p = Process.Start(pInfo); //Wait for window to finish loading. p.WaitForInputIdle(); //Wait for the process to exit or time out. p.WaitForExit(timeOut); //Check to see if the process is still running. if (p.HasExited == false) //Process is still running. //Test to see if the process is hung up. if (p.Responding) //Process was responding; close the main window. p.CloseMainWindow(); else //Process was not responding; force the process to close. p.Kill(); MessageBox.Show("Code continuing...");