C
C#2mo ago
FusedQyou

✅ 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
FusedQyou
FusedQyou2mo ago
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.
canton7
canton72mo ago
Is _internalCancellationTokenSource ever instantiated? Presumably it is, or this._combinedCancellationTokenSource.Token would throw
FusedQyou
FusedQyou2mo ago
Yes, on line 7, no?
canton7
canton72mo ago
D'oh, I didn't read it properly
FusedQyou
FusedQyou2mo ago
No problem 😛
canton7
canton72mo ago
Why not track down which point the problem happens at? First off, is your event handler even called?
FusedQyou
FusedQyou2mo ago
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?
canton7
canton72mo ago
How are you closing your application?
FusedQyou
FusedQyou2mo ago
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 anything
cap5lut
cap5lut2mo ago
since u use MS.E.H, i think u should use the IHostApplicationLifetime stuff to handle the graceful shutdown instead of that event handler
FusedQyou
FusedQyou2mo ago
I am trying to exit the console application by closing the window and also by stopping the debugging. Both have the same effect
canton7
canton72mo ago
Try Ctrl-C, out of curiosity?
FusedQyou
FusedQyou2mo ago
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.
cap5lut
cap5lut2mo ago
from the shape i thought u r using Microsoft.Extensions.Hosting?
FusedQyou
FusedQyou2mo ago
Well yeah I kind of wrote my own variant of it, but it's just a basic console application
cap5lut
cap5lut2mo ago
oh
FusedQyou
FusedQyou2mo ago
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
cap5lut
cap5lut2mo ago
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)
FusedQyou
FusedQyou2mo ago
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 mean
cap5lut
cap5lut2mo ago
Microsoft.Extensions.Hosting = MS.E.H im just too lazy to write it out all the time ;p
FusedQyou
FusedQyou2mo ago
Ohh 😄 Well, I don't need a whole API and that seems to be the only solution here
cap5lut
cap5lut2mo ago
i mean it comes with proper lifetime handling for everything and wires logging, configuration and DI into it as well
FusedQyou
FusedQyou2mo ago
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
cap5lut
cap5lut2mo ago
u could dig through MS.E.H's source to check how they r doing it
FusedQyou
FusedQyou2mo ago
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.
cap5lut
cap5lut2mo ago
https://github.com/Cysharp/ConsoleAppFramework is an example on how to extent MS.E.H's generic host stuff as well
FusedQyou
FusedQyou2mo ago
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
cap5lut
cap5lut2mo ago
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 sufficient
FusedQyou
FusedQyou2mo ago
I 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 now
cap5lut
cap5lut2mo ago
thats usually the case ;p especially because these frameworks are used everywhere and thus are well tested
FusedQyou
FusedQyou2mo ago
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
cap5lut
cap5lut2mo ago
and now u can work on the actual application 😄
FusedQyou
FusedQyou2mo ago
Thanks for sending this, I'll be sure to use this when possible
cap5lut
cap5lut2mo ago
glad i could help o7