我如何不间断退出控制台应用程序。 (申请在ProcessExit方法完成之前终止)
我有一个.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,显然您可以
在这种情况下进行类似的操作,如果您的应用程序关闭,首先将调用此Processexithandler。我从未尝试过。如果按照打算的方式工作,您可能会在应用程序关闭之前拉动任何您喜欢的东西。
检测何时关闭控制台?
如果那没有帮助,我会检查我会检查这些:
关闭从同种异体开始的控制台会导致我的整个应用退出?我可以更改此行为吗?
捕获控制台出口C#
Ok, so apparently you can do something like this
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#