1

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.)

GrahamN
  • 63
  • 7
  • The process you started is no longer running. It may, for a myriad of reasons, have launched another instance of itself before exiting, and so visually it *appears* that the program you ran is running but in reality it is not. – Damien_The_Unbeliever Mar 24 '20 at 10:44
  • Damien, that's my thought too (which is why I said 'apparently',) but why would mmc.exe do that? It needs elevation, but then so does regedit.exe and that doesn't restart itself under these conditions. – GrahamN Mar 24 '20 at 11:03
  • 1
    Would this answer your question? https://stackoverflow.com/questions/47083917/mmc-process-immediately-closing-cant-link-to-windows-forms – weichch Mar 24 '20 at 11:19
  • Yes, that's it! I'd checked mmc, notepad and regedit, and all appear to be 64-bit. Mine was set to "any CPU". Having forced mine to x64, WaitForExit waits for mmc.exe. Thank you. – GrahamN Mar 24 '20 at 17:33

0 Answers0