How to handle a C# console application terminating?

Posted by Nick R on Stack Overflow See other posts from Stack Overflow or by Nick R
Published on 2010-03-31T11:57:02Z Indexed on 2010/03/31 12:03 UTC
Read the original article Hit count: 379

Filed under:
|

If I have a console application, is there any way I can handle the following:

  1. Ctrl-C (I know the answer to this. Using Console.TreatControlCAsInput and Console.CancelKeyPress)
  2. Session termination, such as when someone logs off
  3. Process exit, such as when someone uses the task manager to close the application.

I know that if I was writing a unix application, I would handle various signals to catch the request to close (SIGTERM from memory), but I also know I need to handle these messages pretty quickly and exit before the system does a kill -9 (SIGKILL).

But for a C# console application, I'm not sure how to do this.

© Stack Overflow or respective owner

Related posts about c#

Related posts about console