终止进程事​​件

发布于 2025-01-08 05:44:42 字数 338 浏览 0 评论 0原文

想象一下启动一个进程或启动一个应用程序,但是在任务管理器中杀死了这个进程。有相关事件吗?

这段代码取消用户关闭窗口应用程序

private void Form1_Aplicacao_FormClosing(object sender, FormClosingEventArgs e)
{
   if (e.CloseReason == CloseReason.UserClosing)
      e.Cancel = false;
}

但是如果我使用 process.Kill() 方法,FormClosing 事件尚未被触发。 我的问题:有一个事件可以杀死这个自己的进程吗?

Imagine launching a process or start an application, however in task manager makes a kill this process. Is there an event associated?

This code, cancel the user close the window application

private void Form1_Aplicacao_FormClosing(object sender, FormClosingEventArgs e)
{
   if (e.CloseReason == CloseReason.UserClosing)
      e.Cancel = false;
}

But if I using process.Kill() method, FormClosing event has not been fired.
My question: Have an event to get a kill this own process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

埋情葬爱 2025-01-15 05:44:42

进程被终止时不会引发任何事件。最有可能的是,当进程被杀死时,只是从调度程序的可运行任务列表中删除,其内存被释放,然后操作系统会拾取任何剩余的部分。我绝对不会指望析构函数或垃圾收集运行,因此操作系统无法清理的任何正在使用的资源都将被泄漏。

(实际上)杀死一个进程意味着你杀掉它,把它切成薄片,磨碎它,然后把碎片扔进大海。从技术上讲,不会跳转到被杀死进程的代码,因此该代码内不会触发任何事件。请记住,事件与许多其他流程控制结构(循环、条件等)以及多任务处理本身一样,只是花哨的 goto。诚然,非常有用花哨的 goto,但仍然只是简单地跳转到可用代码的不同部分。

No event is raised when a process is killed. Most likely, when killed the process is simply removed from the scheduler's list of runnable tasks, its memory is deallocated, and then the OS picks up any remaining pieces afterwards. I would definitely not count on even destructors or garbage collection running, so any resources which are in use that the OS cannot clean up will be leaked.

(Actually) killing a process means that you slay it, slice it up, grind it, and toss the pieces into the ocean. More technically, no jump into the killed process' code is issued, so there can be no event fired inside that code. Remember that events, like a lot of other flow-control constructs (loops, conditionals, etc) as well as multitasking itself, are just fancy gotos. Very useful fancy gotos, admittedly, but still simply jumps to different parts of available code.

陌伤ぢ 2025-01-15 05:44:42

简短的回答:不,您的应用程序无法意识到(例如引发事件)其托管进程已被残酷终止。

Short answer: no, there is no way for your application to be aware (for instance with an event that is raised) that its hosting process has been brutally killed.

書生途 2025-01-15 05:44:42

退出是不对的。
您可以使用 PIPES 等流程通信技术。

您可以发送抛出 PIPES 并等待答复。
您可以创建一个规则,如果在 X 时间内没有收到答案,则进程已被终止。

但这只是进程通信选项之一,有关详细信息,您可以查看以下链接:https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx

另请查看以下选项:
C# process.start,我该怎么做知道进程是否结束?

It's not quit right.
You can use process communication Technics such as PIPES.

You can send throw PIPES and wait for answer.
You can create a rule that if answer hasn't been received in X time, the process has been killed.

But this in just one of the process communication options, for more information you can check in the following link: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx

Also look at the following option:
C# process.start, how do I know if the process ended?

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