This code runs as expected on a large number of machines. However on one particular machine, the call to WaitForExit()
seems to be ignored, and in fact marks the process as exited.
static void Main(string[] args)
{
Process proc = Process.Start("notepad.exe");
Console.WriteLine(proc.HasExited); //Always False
proc.WaitForExit(); //Blocks on all but one machines
Console.WriteLine(proc.HasExited); //**See comment below
Console.ReadLine();
}
Note that unlike a similar question on SO, the process being called is notepad.exe
(for testing reasons), so it is unlikely the fault lies with it - i.e. it is not spawning a second sub-process and closing. Even so, it would not explain why it works on all the other machines.
On the problem machine, the second call to Console.WriteLine(proc.HasExited))
returns true
even though notepad is still clearly open, both on the screen and in the task manager.
The machine is running Windows 7 and .NET 4.0.
My question is; what conditions on that particular machine could be causing this? What should I be checking?
Edit - Things I've tried so far / Updates / Possibly relevant info:
- Reinstalled .NET.
- Closed any processes I don't know in task manager.
- Windows has not yet been activated on this machine.
- Following advice in the comments, I tried getting the 'existing' process Id using
GetProcessesByName
but that simply returns an empty array on the problem machine. Therefore, it's hard to say the problem is even withWaitForExit
, as the process is not returned by callingGetProcessesByName
even before callingWaitForExit
. - On the problem machine, the resulting notepad process's ParentID is the ID of the notepad process the code manually starts, or in other words, notepad is spawning a child process and terminating itself.