简体   繁体   English

WinForm 应用程序 -> 结束进程 -> FormClosing() & FormClosed() 事件未触发

[英]WinForm Application -> End Process -> FormClosing() & FormClosed() events Not Fired

I have a started win-form application with some codes in FormClosing() & FormClosed() events.我在FormClosing()FormClosed()事件中有一些代码的已启动的 win-form 应用程序。
With another application i want to forcibly close that first application like this :对于另一个应用程序,我想像这样强行关闭第一个应用程序:

    string procId = proc.Id.ToString();

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "taskkill.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "/f /t /pid " + procId;
    Process.Start(startInfo);

When i want for forcibly close first application by those codes or using with Task Manager -> End Process , FormClosing() & FormClosed() events are not fired.当我想通过这些代码强行关闭第一个应用程序或与Task Manager -> End Process 一起使用时,不会触发FormClosing()FormClosed()事件。

How can i force those events to fire in every situation or what is the solution?我如何强制这些事件在每种情况下触发,或者解决方案是什么?

See Prevent C# app from process kill . 请参阅防止C#应用程序被进程杀死

You cannot prevent your app's process being terminated, and you won't get an event it is being terminated. 您无法阻止应用程序的进程终止,也不会收到终止该进程的事件。

So you must cater for the case where a process didn't clean up nicely at shutdown. 因此,您必须适应在关机时无法很好地清理进程的情况。 For that matter, you need to do that anyway, what if the machine BSODs while your app is running? 为此,无论如何,您都需要这样做,如果在运行应用程序时机器出现蓝屏死机呢? You can never assume the previous instance completed successfully. 您永远不能假设先前的实例成功完成。

It looks like your app closes some data file upon shutdown, so that the next time the program runs it can read the file again. 看来您的应用在关闭时会关闭一些数据文件,以便程序下次运行时可以再次读取该文件。 If that is the case, you could write some "dirty flag" in or near the file and clean the file up on startup if it is flagged. 在这种情况下,您可以在文件中或文件附近写一些“脏标志”,并在启动时清理文件(如果已标记)。

As explained in How does task manager kill my program? 任务管理器如何终止程序? and MSDN: Terminating a Process (Windows) : MSDN:终止进程(Windows)

Note that you cannot intercept or react upon TerminateProcess. 请注意,您不能拦截或对TerminateProcess作出反应。 Your process will die and there is nothing you can do before that happens. 您的过程将终止,在此之前您无能为力。

If a process is terminated by TerminateProcess, all threads of the process are terminated immediately with no chance to run additional code. 如果某个进程被TerminateProcess终止,则该进程的所有线程将立即终止,而没有机会运行其他代码。 This means that the thread does not execute code in termination handler blocks. 这意味着线程不执行终止处理程序块中的代码。

Maybe a stupid question but why dont you just ask the application to close itself? 也许是一个愚蠢的问题,但是为什么不要求应用程序自行关闭呢? It is pretty easy to build a line of communication between the two apps and let the latter app inform the other app it should terminate itself. 在这两个应用程序之间建立一条通信线,让后一个应用程序通知另一个应用程序应自行终止,这很容易。

then you would have the ability to do a clean shutdown. 那么您将有能力进行彻底关机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM