如何在关闭控制台应用程序时调用方法?

时间:2022-10-18 20:45:07

I've read dozens of existing questions on here, they all come up with the same solutions, unfortunately none of these work. It seems they only work when the X button is clicked on a console application.

我在这里已经阅读了几十个现有的问题,他们都提出了相同的解决方案,不幸的是,这些都没有。它似乎仅在控制台应用程序上单击X按钮时才起作用。

I am asking, is there a way to run some code when the console is forced closed? This means force closing the task in task manager, or the operating system froce closing it for any reason.

我问,有没有办法在强制关闭控制台时运行一些代码?这意味着强制关闭任务管理器中的任务,或者操作系统因任何原因关闭它。

I've tried pretty much every existing solution on open questions of this site. This is not a duplicate. I repeat, this is not a duplicate. It's simply asking for something totally different.

我在本网站的开放性问题上尝试了几乎所有现有的解决方案。这不是重复的。我再说一遍,这不是重复的。它只是要求完全不同的东西。

Have I tried anything so far? No, I've tried all the usual, assigning a callback function to all kinds of events, but nothing actually works. I've done this before in a WinForm, but can't in console.

到目前为止我有没有尝试过什么不,我已经尝试了所有常规的,为各种事件分配回调函数,但没有任何实际工作。我之前在WinForm中做过这个,但是不能在控制台中。

1 个解决方案

#1


0  

You may be looking for Application Recovery & Restart. The Application Recovery and Restart (ARR) technologies enable developers to customize an application's behavior when WER terminates the application due to an unrecoverable error.

您可能正在寻找应用程序恢复和重新启动。应用程序恢复和重启(ARR)技术使开发人员能够在WER因不可恢复的错误而终止应用程序时自定义应用程序的行为。

Refer: https://msdn.microsoft.com/en-us/library/cc303699.aspx

-- Or for a more general and reliable fix --

- 或者更一般和可靠的解决方案 -

First application can start a second application (if not already running in background). You can use this second application (running as a background service) to "watch" the first application which can start it again on close. You can modify the behavior based on clean exit of first application as well. Very simple approach to start the process would be:

第一个应用程序可以启动第二个应用程序(如果尚未在后台运行)。您可以使用此第二个应用程序(作为后台服务运行)来“监视”第一个可以在关闭时再次启动它的应用程序。您也可以根据第一个应用程序的干净退出修改行为。启动流程的非常简单的方法是:

    // Wait for the process to terminate
    Process process = null;
    try
    {
        process = Process.GetProcessById(pid);
        process.WaitForExit(1000);
    }
    catch (Exception ex)
    {
        // Handle appropriately
    }
    Process.Start(applicationName, "");

#1


0  

You may be looking for Application Recovery & Restart. The Application Recovery and Restart (ARR) technologies enable developers to customize an application's behavior when WER terminates the application due to an unrecoverable error.

您可能正在寻找应用程序恢复和重新启动。应用程序恢复和重启(ARR)技术使开发人员能够在WER因不可恢复的错误而终止应用程序时自定义应用程序的行为。

Refer: https://msdn.microsoft.com/en-us/library/cc303699.aspx

-- Or for a more general and reliable fix --

- 或者更一般和可靠的解决方案 -

First application can start a second application (if not already running in background). You can use this second application (running as a background service) to "watch" the first application which can start it again on close. You can modify the behavior based on clean exit of first application as well. Very simple approach to start the process would be:

第一个应用程序可以启动第二个应用程序(如果尚未在后台运行)。您可以使用此第二个应用程序(作为后台服务运行)来“监视”第一个可以在关闭时再次启动它的应用程序。您也可以根据第一个应用程序的干净退出修改行为。启动流程的非常简单的方法是:

    // Wait for the process to terminate
    Process process = null;
    try
    {
        process = Process.GetProcessById(pid);
        process.WaitForExit(1000);
    }
    catch (Exception ex)
    {
        // Handle appropriately
    }
    Process.Start(applicationName, "");