为什么Process.WaitForExit不等待?
static void Main(string[] args)
{
while (true)
{
try
{
if (Process.GetProcessesByName("ServerAPPFileManager").Length > 0)
{
Process clsProcess = Process.GetProcessesByName("ServerAPPFileManager")[0];
Console.WriteLine(clsProcess.ProcessName);
clsProcess.WaitForExit();
}
if (System.IO.File.Exists(System.IO.Path.Combine(Environment.CurrentDirectory, "ServerAPPFileManager.exe")))
{
Console.WriteLine("File Found");
Process p = new Process();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName = System.IO.Path.Combine(Environment.CurrentDirectory, "ServerAPPFileManager.exe");
p.Start();
}
}
catch(Exception e)
{
Console.Write(e.Message);
System.Threading.Thread.Sleep(1000);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它不断循环并一次又一次地启动应用程序!
编辑:
它现在有效,它没有工作,因为它就像这样 clsProcess.WaitForExit(1000);
这个代码被包裹在一个while(true)循环,也没有break,throw或return在循环的主体内的语句.它确实会导致无限循环.
如果你想在WaitForExit完成后退出循环,那么你需要引入一个break语句来完成.
clsProcess.WaitForExit();
break;
Run Code Online (Sandbox Code Playgroud)