掌握控制台退出C# [英] Capture console exit C#

查看:196
本文介绍了掌握控制台退出C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含相当多线程的控制台应用程序。有迹象表明,监测特定的条件和终止程序,如果他们是真正的线程。这种终止可以在任何时候发生。

我需要一个可以当程序关闭被触发的事件,这样我就可以清理所有其他线程,并关闭所有文件句柄和连接正常。我不知道如果有一个已经内置在.NET框架,所以我问之前,我写我自己。

我在想,如果有大致为一个事件:

  MyConsoleProgram.OnExit + = CleanupBeforeExit;
 

解决方案

我不知道,我在网上找到的code,但我在我的老项目之一找到了现在。这将允许你做清理code。在您的控制台,如当它突然关闭或由于关闭...

  [的DllImport(Kernel32中)
私人静态外部布尔SetConsoleCtrlHandler(事件处理程序处理,布尔补充);

私人代表布尔事件处理程序(CtrlType SIG);
静态的事件处理程序_handler;

枚举CtrlType
{
  CTRL_C_EVENT = 0,
  CTRL_BREAK_EVENT = 1,
  CTRL_CLOSE_EVENT = 2,
  CTRL_LOGOFF_EVENT = 5,
  CTRL_SHUTDOWN_EVENT = 6
}

私有静态布尔处理器(CtrlType SIG)
{
  开关(SIG)
  {
      案例CtrlType.CTRL_C_EVENT:
      案例CtrlType.CTRL_LOGOFF_EVENT:
      案例CtrlType.CTRL_SHUTDOWN_EVENT:
      案例CtrlType.CTRL_CLOSE_EVENT:
      默认:
          返回false;
  }
}


静态无效的主要(字串[] args)
{
  //一些biolerplate作出反应,关闭窗口事件
  _handler + =新的EventHandler(处理程序);
  SetConsoleCtrlHandler(_handler,真正的);
  ...
}
 

更新

对于那些不检查的意见看来,这个特殊的解决方案确实的不可以很好地工作(或全部)在 Windows 7中。关于这个以下螺纹会谈:<一href="http://social.msdn.microsoft.com/Forums/en/windowscompatibility/thread/abf09824-4e4c-4f2c-ae1e-5981f06c9c6e">http://social.msdn.microsoft.com/Forums/en/windowscompatibility/thread/abf09824-4e4c-4f2c-ae1e-5981f06c9c6e

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.

I was wondering if there was an event along the lines of:

MyConsoleProgram.OnExit += CleanupBeforeExit;

解决方案

I am not sure where I found the code on the web, but I found it now in one of my old projects. This will allow you to do cleanup code in your console, e.g. when it is abruptly closed or due to a shutdown...

[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);
  ...
}

Update

For those not checking the comments it seems that this particular solution does not work well (or at all) on Windows 7. The following thread talks about this: http://social.msdn.microsoft.com/Forums/en/windowscompatibility/thread/abf09824-4e4c-4f2c-ae1e-5981f06c9c6e

这篇关于掌握控制台退出C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆