why Process.WaitForExit doesn’t wait?
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);
}
}
}
it keeps looping and starting the application again and again!
Edit:
It worked now, It was not working because it was like this clsProcess.WaitForExit(1000);
This code is wrapped in a
while(true)loop and there are nobreak,throworreturnstatements within the body of the loop. It will indeed result in an infinite loop.If you want to exit the loop once the
WaitForExithas finished then you need to introduce abreakstatement to do so.