Sometimes Process.WaitForExit() apparently doesn't wait. Take the following C# code snippet (which runs elevated):
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"C:\WINDOWS\system32\mmc.exe"; // WaitForExit does not wait
start.FileName = @"C:\WINDOWS\system32\notepad.exe"; // WaitForExit waits
start.FileName = @"C:\WINDOWS\system32\regedit.exe"; // WaitForExit waits
start.UseShellExecute = false;
// Fire off the process
var process = Process.Start(start);
int exitCode = 0;
process.WaitForExit();
exitCode = process.ExitCode;
Of course, only one of the start.Filename assigments will be present at a time. (The others just commented out.)
When only mmc.exe is present, Process.WaitForExit() does not wait. When either of the other two are present instead, Process.WaitForExit() waits.
So, two questions. Why doesn't Process.WaitForExit() wait for mmc.exe, and is there any way to make it wait? (FWIW specifying start.CreateNoWindow = true makes no difference.)