如何检测应用程序被杀?
想象一下,我们有两个 .net 应用程序。应用程序“A”使用 System.Diagnostics.Process 类启动应用程序“B”。然后“A”想通过Process.Kill
方法杀死“B”。 “B”如何确定有人正在杀他?
Imagine, we have two .net applications. Application "A" starts application "B" using System.Diagnostics.Process
class. Then "A" wants to kill "B" by method Process.Kill
.
How "B" can determine that somebody is killing him?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为应用程序不可能响应被杀死...我认为它更多地在操作系统级别运行,就像使用任务管理器时一样。
在这种情况下使用 Process.Kill() 可能不正确,您能否提供有关您要解决的问题的更多信息?
I don't think it's possible for the application to respond to being killed... I think it operates more at the OS level like when using task manager.
Using Process.Kill() might not be right in this context, can you provide more information about the problem you are trying to solve?
也许你可以在进程 B 的代码中尝试这种方式...
我不能自信地说这会起作用...向进程发送“Kill”的操作系统的本质是依赖于实现的,因此,有并不能保证进程 B 知道它正在被杀死的万无一失的方法。由于您没有明确说明进程 B 是否是托管/非托管进程,因此我将假设它确实是托管的,因为标签是“.net”,如果它是 WinForm 应用程序,则可能是
Closing winForms 中的
事件在事件处理程序的参数中会有一个原因,或者使用 ApplicationDomain 实例,如下所示:希望这有帮助,
此致,
汤姆.
Maybe you could try it this way within Process B's code...
I cannot say with confidence that this will work...the very nature of the OS that sends a 'Kill' to a process is implementation dependant, and as such, there is no guaranteed foolproof way of Process B in knowing that it is being killed. As you have not stated explicitly if process B is a managed/unmanaged process, I will make the basis of the assumption that it is indeed managed as the tag is '.net', If it's a WinForm application, perhaps a
Closing
event within winForms will have a reason in that event handler's argument or use an ApplicationDomain instance as shown below:Hope this helps,
Best regards,
Tom.