检测控制台何时关闭?

0 .net c# console

我用谷歌搜索,发现Console.CancelKeyPress哪些是有用的,但很多次我通过关闭窗口关闭我的控制台.使用右上角的X. Console.CancelKeyPress检测到ctrl + c,如何通过点击X来检测关闭?

Nic*_*rey 5

试试这个:

class Program
{
    static void Main( string[] args )
    {
        AppDomain.CurrentDomain.ProcessExit += ProcessExitHandler ;
    }

    static void ProcessExitHandler( object sender , EventArgs e )
    {
        throw new NotImplementedException("You can't shut me down. I quit!" ) ;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑注意:显然,这和大多数其他技术已经在Windows 7中失去了WRT控制台应用程序.当窗口关闭时,控制台应用程序被强制终止,因此应用程序永远不会发出信号.谢谢MS!

http://social.msdn.microsoft.com/Forums/en/windowscompatibility/thread/abf09824-4e4c-4f2c-ae1e-5981f06c9c6e

解决方案似乎是(参见上面的URL)使您的控制台应用程序成为一个Windows应用程序,其中有一个处理消息的不可见窗口WM_ENDSESSION,该处理程序在关闭之前将获得5秒终止.

  • @ acidzombie24:显然,Windows 7和Vista破坏了......错误......改进了一切.请参阅我编辑的答案了解详情. (2认同)