如何检测应用程序被杀?

发布于 2024-08-21 15:16:23 字数 133 浏览 7 评论 0原文

想象一下,我们有两个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-08-28 15:16:23

我认为应用程序不可能响应被杀死...我认为它更多地在操作系统级别运行,就像使用任务管理器时一样。

在这种情况下使用 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?

强辩 2024-08-28 15:16:23

也许你可以在进程 B 的代码中尝试这种方式...

// We're in Process B....
System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
proc.EnableRaisingEvents = true;
proc.Exited += new EventHandler(proc_Exited);

static void proc_Exited(object sender, EventArgs e)
{
   // Handle here that we got killed...
}

我不能自信地说这会起作用...向进程发送“Kill”的操作系统的本质是依赖于实现的,因此,有并不能保证进程 B 知道它正在被杀死的万无一失的方法。由于您没有明确说明进程 B 是否是托管/非托管进程,因此我将假设它确实是托管的,因为标签是“.net”,如果它是 WinForm 应用程序,则可能是 Closing winForms 中的 事件在事件处理程序的参数中会有一个原因,或者使用 ApplicationDomain 实例,如下所示:

AppDomain thisDom = AppDomain.CurrentDomain;
thisDom.ProcessExit += new EventHandler(thisDom_ProcessExit);
//

static void thisDom_ProcessExit(object sender, EventArgs e)
{
   // Handle the situation here where the AppDomain is going to be unloaded and killed!
}

希望这有帮助,
此致,
汤姆.

Maybe you could try it this way within Process B's code...

// We're in Process B....
System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
proc.EnableRaisingEvents = true;
proc.Exited += new EventHandler(proc_Exited);

static void proc_Exited(object sender, EventArgs e)
{
   // Handle here that we got killed...
}

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:

AppDomain thisDom = AppDomain.CurrentDomain;
thisDom.ProcessExit += new EventHandler(thisDom_ProcessExit);
//

static void thisDom_ProcessExit(object sender, EventArgs e)
{
   // Handle the situation here where the AppDomain is going to be unloaded and killed!
}

Hope this helps,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文