2

I'm writing a new Console application in .NET Core that has to work crossplatform. When somebody closes the program - in whatever way - the program should save a lot of data. So I tried to find a way to detect whether the program gets closed. This is what I tried:

AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => Console.WriteLine("test");

However, when I run my program in Rider and use control+c, it doesn't write test.

How do I correctly detect whether the program is closed (cross-platform!) and execute code before completely closing the program?

Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63

0 Answers0