I'm facing the following situation. I have a process that gets crashed constantly. Mostly it crashes 0xC0000374 error code. However, after hundreds of crashes, another one occurs which does not result the process to get removed from the system. Here is the code I'm using
var process = new Process
{
StartInfo =
{
FileName = path,
UseShellExecute = false,
CreateNoWindow = true
},
EnableRaisingEvents = true
};
process.Exited += (s) => ProcessExitedHandler(process);
Here is how the ProcessExitedHandler looks
private static void ProcessExitedHandler(Process process)
{
try
{
var exitCode = process.WaitForExit(1000)
? process.ExitCode
: -1;
....
}
catch (Exception ex)
{
// Something bad happened.
...
}
finally
{
...
}
}
Previously, I noticed that sometimes accessing "ExitCode" in this method throws InvalidOperationException ("Process must exit before requested information can be determined."). I've added "process.WaitForExit" with 1 second timeout, but it seems the process just never exits. The "exitCode" parameter is being set to -1 in this case. If I would just have "process.WaitForExit" without any parameter, I believe it would hang there forever.
I checked my logging system and it seems the process (verified with logged process ID) still hangs there in Windows even after few hours after Process.Exited event is called.
How this can happen? Can the Process.Exited event get fired without the process actually being exited? If yes, what cases that can happen? Deadlock? What is the recommended approach in that case?