我如何不间断退出控制台应用程序。 (申请在ProcessExit方法完成之前终止)

发布于 2025-01-26 09:18:59 字数 820 浏览 2 评论 0原文

我有一个.NET 5.0控制台应用程序。我在任务调度程序中安排了该应用程序EXE。 有时,当我结束任务时,应用程序会杀死,而没有完成我编码要完成的零件。 (如果应用程序退出,我正在停止一些服务,并在文件中写东西。它需要一点时间。)

我尝试了以下命令,但无法正常工作。它正在做同样的事情。

taskkill /t /f /im "ApplicationEXEName.exe"

如果申请退出,我正在寻找一种方法来完成我的操作。

当应用程序主机创建这样的应用程序时,我曾尝试设置关闭时间。但这也行不通。

var host = Host.CreateDefaultBuilder()
            .ConfigureAppConfiguration(ConfigConfiguration)
            .ConfigureServices((context, services) =>
            {
                services.Configure<HostOptions>(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(15));
                ConfigureDefaultServices(services);
                ConfigureServices(configuration, services);                 
            })
            .UseSerilog(Log.Logger, true)
            .Build();

I have a .net 5.0 console application. I scheduled that application exe in task scheduler.
Sometimes, when I end the task, the application kills without finishing the parts I have coded to finish. (I am stopping some services, and writing things in a file if the application exiting. It needs a little bit of time.)

I tried the following command but not working. It is doing the same things.

taskkill /t /f /im "ApplicationEXEName.exe"

I am looking for a way to do wait to finish my actions if application exiting.

I had tried setting ShutdownTimeout when the application host creating like this. But it also not working.

var host = Host.CreateDefaultBuilder()
            .ConfigureAppConfiguration(ConfigConfiguration)
            .ConfigureServices((context, services) =>
            {
                services.Configure<HostOptions>(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(15));
                ConfigureDefaultServices(services);
                ConfigureServices(configuration, services);                 
            })
            .UseSerilog(Log.Logger, true)
            .Build();

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

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

发布评论

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

评论(1

滥情哥ㄟ 2025-02-02 09:18:59

好的,显然您可以

class Program
{
    static void Main( string[] args )
    {
        AppDomain.CurrentDomain.ProcessExit += ProcessExitHandler ;
    }

    static void ProcessExitHandler( object sender , EventArgs e )
    {
        throw new NotImplementedException("You can't shut me down. I quit!" ) ;
    }
}

在这种情况下进行类似的操作,如果您的应用程序关闭,首先将调用此Processexithandler。我从未尝试过。如果按照打算的方式工作,您可能会在应用程序关闭之前拉动任何您喜欢的东西。

检测何时关闭控制台?

如果那没有帮助,我会检查我会检查这些:

关闭从同种异体开始的控制台会导致我的整个应用退出?我可以更改此行为吗?

捕获控制台出口C#

Ok, so apparently you can do something like this

class Program
{
    static void Main( string[] args )
    {
        AppDomain.CurrentDomain.ProcessExit += ProcessExitHandler ;
    }

    static void ProcessExitHandler( object sender , EventArgs e )
    {
        throw new NotImplementedException("You can't shut me down. I quit!" ) ;
    }
}

in which case, if your application is closing, firstly this ProcessExitHandler will be called. I have never tried it though. If it works the way it is intended you might be able to pull of anything you like before the application closes.

Detect when console is being closed?

if that doesn't help I would check these:

Why does closing a console that was started with AllocConsole cause my whole application to exit? Can I change this behavior?

Capture console exit C#

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