我有一个包含很多线程的控制台应用程序.有些线程可以监视某些条件,如果它们是真的则终止程序.这种终止可以随时发生.
我需要一个可以在程序关闭时触发的事件,以便我可以清理所有其他线程并正确关闭所有文件句柄和连接.我不确定.NET框架中是否已经内置了一个,所以我在编写自己的框架之前就已经问过了.
我想知道是否有一个事件:
MyConsoleProgram.OnExit += CleanupBeforeExit;
flq.. 92
我不确定我在网上找到代码的位置,但我现在在我的一个旧项目中找到了它.这将允许您在控制台中执行清理代码,例如,当它突然关闭或由于关闭时...
[DllImport("Kernel32")] private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add); private delegate bool EventHandler(CtrlType sig); static EventHandler _handler; enum CtrlType { CTRL_C_EVENT = 0, CTRL_BREAK_EVENT = 1, CTRL_CLOSE_EVENT = 2, CTRL_LOGOFF_EVENT = 5, CTRL_SHUTDOWN_EVENT = 6 } private static bool Handler(CtrlType sig) { switch (sig) { case CtrlType.CTRL_C_EVENT: case CtrlType.CTRL_LOGOFF_EVENT: case CtrlType.CTRL_SHUTDOWN_EVENT: case CtrlType.CTRL_CLOSE_EVENT: default: return false; } } static void Main(string[] args) { // Some biolerplate to react to close window event _handler += new EventHandler(Handler); SetConsoleCtrlHandler(_handler, true); ... }
更新
对于那些没有检查评论的人来说,似乎这个特定的解决方案在Windows 7上运行不佳(或根本没有).以下主题讨论了这一点
我不确定我在网上找到代码的位置,但我现在在我的一个旧项目中找到了它.这将允许您在控制台中执行清理代码,例如,当它突然关闭或由于关闭时...
[DllImport("Kernel32")] private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add); private delegate bool EventHandler(CtrlType sig); static EventHandler _handler; enum CtrlType { CTRL_C_EVENT = 0, CTRL_BREAK_EVENT = 1, CTRL_CLOSE_EVENT = 2, CTRL_LOGOFF_EVENT = 5, CTRL_SHUTDOWN_EVENT = 6 } private static bool Handler(CtrlType sig) { switch (sig) { case CtrlType.CTRL_C_EVENT: case CtrlType.CTRL_LOGOFF_EVENT: case CtrlType.CTRL_SHUTDOWN_EVENT: case CtrlType.CTRL_CLOSE_EVENT: default: return false; } } static void Main(string[] args) { // Some biolerplate to react to close window event _handler += new EventHandler(Handler); SetConsoleCtrlHandler(_handler, true); ... }
更新
对于那些没有检查评论的人来说,似乎这个特定的解决方案在Windows 7上运行不佳(或根本没有).以下主题讨论了这一点
完整的工作示例,使用ctrl-c,用X关闭窗口并杀死:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; namespace TestTrapCtrlC { public class Program { static bool exitSystem = false; #region Trap application termination [DllImport("Kernel32")] private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add); private delegate bool EventHandler(CtrlType sig); static EventHandler _handler; enum CtrlType { CTRL_C_EVENT = 0, CTRL_BREAK_EVENT = 1, CTRL_CLOSE_EVENT = 2, CTRL_LOGOFF_EVENT = 5, CTRL_SHUTDOWN_EVENT = 6 } private static bool Handler(CtrlType sig) { Console.WriteLine("Exiting system due to external CTRL-C, or process kill, or shutdown"); //do your cleanup here Thread.Sleep(5000); //simulate some cleanup delay Console.WriteLine("Cleanup complete"); //allow main to run off exitSystem = true; //shutdown right away so there are no lingering threads Environment.Exit(-1); return true; } #endregion static void Main(string[] args) { // Some boilerplate to react to close window event, CTRL-C, kill, etc _handler += new EventHandler(Handler); SetConsoleCtrlHandler(_handler, true); //start your multi threaded program here Program p = new Program(); p.Start(); //hold the console so it doesn’t run off the end while (!exitSystem) { Thread.Sleep(500); } } public void Start() { // start a thread and start doing some processing Console.WriteLine("Thread started, processing.."); } } }
检查还:
AppDomain.CurrentDomain.ProcessExit