C#Hotkey退出控制台应用程序

时间:2014-07-07 12:22:57

标签: c# console-application

所以我正在寻找设置热键以便能够退出控制台应用程序的方法。 在我的路上,我找到了这个帖子:Global hotkey in console application 这对我帮助很大。 所以基本上我设置了一个退出我的应用程序的热键。我的代码如下所示:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleHotKey
{
  class Program
  {
    static void Main(string[] args)
    {
      HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt);
      HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
      Console.ReadLine();      
    }

    static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
    {
      Environment.Exit(1);
    }
  }
}

我的问题是:这是退出这样的控制台应用程序的好方法吗? 我发现有些人说这不是一个好方法,但我不明白为什么。 有人可以给我一些澄清吗?

0 个答案:

没有答案
相关问题