无限期等待外壳应用程序完成 下面的代码示例启动另一个应用程序(本例中是 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..."); 为外壳应用程序设置超时下面的代码示例为外壳应用程序设置了超时。示例中的超时设置为 5 秒。在测试中您可能希望调整此数字(以毫秒计)。 //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...");