Shutdown Event for C # Console Application

How can I find out when my console application in C # will stop? Is there any event or something similar?

Thanks!

+6
source share
2 answers

Use ProcessExit application domain event

class Program { static void Main(string[] args) { AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); } static void CurrentDomain_ProcessExit(object sender, EventArgs e) { Console.WriteLine("exit"); } } 
+23
source

Handling the System.Console.CancelKeyPress event can help you.

MSDN explains how to handle this event along with other things you need to take care of when handling this event, excerpt:

This event is used in conjunction with System.ConsoleCancelEventHandler and System.ConsoleCancelEventArgs. The CancelKeyPress event allows the console application to intercept the CTRL + C signal so that the application can decide whether to continue or terminate.

Use this event to explicitly control how your application responds to the CTRL + C signal. If your application has simple requirements, you can use the TreatControlCAsInput property instead of this event.

The event handler for this event runs in the thread pool thread.

+5
source

Source: https://habr.com/ru/post/919488/


All Articles