简体   繁体   English

如何处理C#控制台应用程序终止?

[英]How to handle a C# console application terminating?

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) Ctrl-C(我知道答案。使用Console.TreatControlCAsInput和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). 我知道如果我正在编写一个unix应用程序,我会处理各种信号以捕获关闭请求(来自内存的SIGTERM),但我也知道我需要很快处理这些消息并在系统执行kill -9之前退出(SIGKILL)。

But for a C# console application, I'm not sure how to do this. 但对于C#控制台应用程序,我不知道如何做到这一点。

Session termination, such as when someone logs off 会话终止,例如有人注销时

Handle the SystemEvents.SessionEnded event. 处理SystemEvents.SessionEnded事件。

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. 如果你的意思是,如果有人从任务栏中杀死了应用程序,我认为你不能处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM