windows 在 .NET 中检测进程崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2309836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting process crash in .NET
提问by Rubinsh
Is there a way to determine that a process that my program started has crashed?
Currently, the solution I found is to look at Process.ExitCode
and examine the value:
有没有办法确定我的程序启动的进程已经崩溃?目前,我找到的解决方案是查看Process.ExitCode
并检查值:
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.
我更喜欢 C# 中的答案,使用 P/Invoke 也很好。
P.S - I need to work on windows XP/Vista/7
PS - 我需要在 Windows XP/Vista/7 上工作
采纳答案by Benjamin Podszun
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.
除非您能以某种方式影响进程(将错误写入事件日志?文件),据我所知,您无法通过外部方式知道进程崩溃了。对你来说,这只是一个消失的过程。甚至 Windows 也不会在我知道的任何地方保留这些信息。它不再存在,除非该过程以某种方式保留了细节,否则它们已经消失了。
回答by mrranstrom
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.
当进程崩溃时,Windows 首先检查您的系统上是否配置了实时调试器。如果是这样,您可以让调试器在崩溃之前将其自身附加到进程。通常,您会使用此功能在进程崩溃时生成内存转储。您附加的任何调试器都会知道崩溃进程的 PID 和名称。您可以利用现有调试工具(例如 ADPlus)的功能,或者编写自己的程序并告诉 Windows 这是您的即时调试器,应该在进程崩溃时运行。我相信您可以专门为任何进程名称设置 JIT 调试器。
See http://msdn.microsoft.com/en-us/library/5hs4b7a6(v=VS.80).aspx
请参阅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.
我认为,如果您将 HKLM\Software\Microsoft\Windows NT\Current Version\AeDebug\Debugger 注册表项设置为 '"DirectoryOfYourProgram\YourProgram.exe" -p %ld',其中 YourProgram.exe 期望 PID 与 - p 标志,您的程序将在进程崩溃时被调用并传递正确的 PID。
回答by Igor
For Windows 7, there is Application Restart and Recovery API. For .NET you can use Windows API Code Pack.
对于 Windows 7,有Application Restart and Recovery API。对于 .NET,您可以使用Windows API 代码包。
Generaly, you can periodically search for process existence (watchdog application).
通常,您可以定期搜索进程是否存在(看门狗应用程序)。