开发者

How to handle a C# console application terminating?

开发者 https://www.devze.com 2022-12-25 10:01 出处:网络
If I have a console application, is there any way I can handle the following: Ctrl-C (I know the answer to this. Using Console.TreatControlCAsInput and Console.CancelKeyPress)

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 开发者_JS百科not sure how to do this.


Session termination, such as when someone logs off

Handle the SystemEvents.SessionEnded event.

Process exit, such as when someone uses the task manager to close the application.

If you mean, if someone kills the application from the taskbar, I dont think you can handle that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号