捕獲控制檯退出 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章