开发者

Detecting process crash in .NET

Is there a way to determine that a process that my program started开发者_如何学C has crashed? Currently, the solution I found is to look at Process.ExitCode and examine the value:

        this.STProcess = Process.Start(this.CreateProcessStartInfo());
        this.STProcess.WaitForExit();
        if (STProcess.ExitCode != 0)
        {
            //raise error event...
        }

I wanted to know if there is a more elegant (and accurate) way of doing this?

I would prefer answers in C# and using P/Invoke is fine as well.

P.S - I need to work on windows XP/Vista/7


No.

Unless you can influence the process in some way (writing errors to the Eventlog? A file) you have no external way to know that a process crashed, as far as I know. For you it's just a process that went away. Not even Windows is going to keep this information anywhere I know. It doesn't exist anymore and unless the process somehow kept the details, they are gone.


For Windows 7, there is Application Restart and Recovery API. For .NET you can use Windows API Code Pack.

Generaly, you can periodically search for process existence (watchdog application).


When a process crashes, Windows first checks to see if a Just-In-Time debugger is configured on your system. If so, you can have that debugger attach itself to the process right before it crashes. Usually you would use this functionality to generate a memory dump as a process crashes. Whatever debugger you attach gets to know the PID and name of the crashing process. You can either utilize features of existing debugging tools, such as ADPlus, or write your own program and tell Windows that that is your Just-In-Time debugger and should be run when a process crashes. I believe you can set up a JIT debugger specifically for any process name.

See http://msdn.microsoft.com/en-us/library/5hs4b7a6(v=VS.80).aspx

I think that if you set the HKLM\Software\Microsoft\Windows NT\Current Version\AeDebug\Debugger registry entry to '"DirectoryOfYourProgram\YourProgram.exe" -p %ld' where YourProgram.exe expects a PID passed in with the -p flag, your program will be called and passed the correct PID when a process crashes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜