捕获控制台退出 C# - Capture console exit C#

问题:

I have a console application that contains quite a lot of threads.我有一个包含相当多线程的控制台应用程序。 There are threads that monitor certain conditions and terminate the program if they are true.有些线程会监视某些条件并在它们为真时终止程序。 This termination can happen at any time.这种终止可能随时发生。

I need an event that can be triggered when the program is closing so that I can cleanup all of the other threads and close all file handles and connections properly.我需要一个可以在程序关闭时触发的事件,以便我可以清理所有其他线程并正确关闭所有文件句柄和连接。 I'm not sure if there is one already built into the .NET framework, so I'm asking before I write my own.我不确定 .NET 框架中是否已经内置了一个,所以我在编写自己的框架之前先询问一下。

I was wondering if there was an event along the lines of:我想知道是否有这样的事件:

MyConsoleProgram.OnExit += CleanupBeforeExit;

解决方案:

参考: https://stackoom.com/en/question/1zU7
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章