✅ How do I gracefully asynchronously exit a console application?
I am working on a console application that runs an asynchronous method until the console application exits. I want to gracefully exit this method and handle remaining work before fully closing.
The code currently looks like this (only relevant code shared)
Program.cs
: https://gdl.space/acabuvokev.cs
BotApplication.cs
: https://gdl.space/tumeduveko.cs
Expected: The console logs Stopped application.
.
Actual: The last message was Starting bot.
.
Is this possible at all? I want to add additional logic below before the console is exited, but I can't figure out the correct way of doing this.34 Replies
Perhaps the way I check for exit is wrong, but even then it seems unlikely the console application waits the way I expect it to.
Is _internalCancellationTokenSource ever instantiated?
Presumably it is, or
this._combinedCancellationTokenSource.Token
would throwYes, on line 7, no?
D'oh, I didn't read it properly
No problem 😛
Why not track down which point the problem happens at? First off, is your event handler even called?
Doesn't seem to be the case. Neither a breakpoint is hit, nor do I see the console being written to when adding a
Console.WriteLine
Is AppDomain.CurrentDomain.ProcessExit
a proper way to check for an exit?How are you closing your application?
If I were to add a log under
app.RunAsync
then that isn't being displayed either btw, so it just seems to exit out before finishing anythingsince u use
MS.E.H
, i think u should use the IHostApplicationLifetime stuff to handle the graceful shutdown instead of that event handlerI am trying to exit the console application by closing the window and also by stopping the debugging. Both have the same effect
Try Ctrl-C, out of curiosity?
Doesn't seem to log anything either. Just exits the application without firing the event or writing any other logging I added that I would expect
Is what I want even possible?
This doesn't seem to be included by default with Console applications.
from the shape i thought u r using
Microsoft.Extensions.Hosting
?Well yeah I kind of wrote my own variant of it, but it's just a basic console application
oh
So uhhhhhh
Well online isn't really helpful either
I believe a console application just shuts down without any sort of graceful process I think
I kind of hoped there was a way
did u try setting a break point at ur event handler to check if its triggered?
could be that the process stops before the whole output is written to the terminal
(tho i would simply use MS.E.H)
Yes, it didn't hit
Sorry, what is
MS.E.H
?
Googling it doesn't seem to give any result
Well, source generators, but I guess that's not what you meanMicrosoft.Extensions.Hosting
= MS.E.H
im just too lazy to write it out all the time ;pOhh 😄
Well, I don't need a whole API and that seems to be the only solution here
i mean it comes with proper lifetime handling for everything and wires logging, configuration and DI into it as well
Unless there is a project template that actually doesn't need this
But yes, you are right. Just hoped I could have my own "Builder" but a lot that the web application builders use from .NET is internal code and now I have this issue
Good question. An API does have a proper flow for
CTRL+C
for example.
Right, I see HostApplicationBuilder
contains most of the features I probably need so it might be nice to inherit from that instead.
Thanks a lot, I'll see if this improves the application how I want it to.https://github.com/Cysharp/ConsoleAppFramework is an example on how to extent MS.E.H's generic host stuff as well
Thanks, that's super helpful!
I have been digigng through the .NET builders but with how much they do it sure takes some time to get used to it
But this library seems to be a lot more readable
tbh u dont even have to extend the generic host stuff like
ConsoleAppFramework
does, unless u want to write a framework urself.
if u just want to write some kind of application, the generic host itself is sufficientI have refactored the project to use this whilst also looking at the source code and I have to say this is miles upon miles easier than reinventing the wheel and doing it myself 😎
I see the host has a special
UseConsoleLifetime
which hooks the app cancellation the same way I tried but I guess I made a mistake somewhere because theirs actually works
Either way the cancellation works properly nowthats usually the case ;p
especially because these frameworks are used everywhere and thus are well tested
Indeed, I realized it was very modular but I didn't realize you can literally just hook the host on your project and use that instead
and now u can work on the actual application 😄
Thanks for sending this, I'll be sure to use this when possible
glad i could help o7