0

So i was searching a way to set a hotkey to be able to exit a console application. On my way i found this thread: Global hotkey in console application which helped me a lot. So basically i am setting a hotkey to exit my application. My code looks like this:

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

and my question is: Is it a good way to exit a console application like this? I found some poeple saying that it is not such a good way but i couldn't understand why. Can someone give me some clarifications please?

Community
  • 1
  • 1
user2530266
  • 287
  • 3
  • 18
  • possible duplicate of [When should one use Environment.Exit to terminate a console application?](http://stackoverflow.com/questions/692323/when-should-one-use-environment-exit-to-terminate-a-console-application) – sertsedat Jul 07 '14 at 12:24
  • What if by mistake someone press the hotkey? – Rahul Jul 07 '14 at 12:25
  • I think it's not good because application can be interrupted at unexpected points. As a result, data can be left in corrupted state (you were planned to update it in the next line of code, but the application was interrupted between two lines). You should allow exitting application only when state is stable and valid. If at any point you are sure this is the case, then I don't see any problems with this solution. – Sasha Jul 07 '14 at 12:31
  • 1
    Is shutting down your console application something that users are likely to want to do whilst in the middle of interacting with some other piece of software? If not, a *global* solution seems out of place. Why not just react to e.g. `CTRL-C` when your console window is focussed, the standard way to signal to console applications that they should shut down? – Damien_The_Unbeliever Jul 07 '14 at 12:36
  • I am going to use it with care of course. I am not going to interrupt the application at unexpected points. It is for personal use so this is not such a problem. I am just asking because i saw people saying that it is not such a good way and i can not understand why. Obviously it can cause memory leak if it is not used correctly but this is not a problem since i am the ony one who will be using it. – user2530266 Jul 07 '14 at 12:38
  • A debug for this in case i interrupt it while it is running, since i am using keys and some of them maybe held down, is to create a small function that will release all the keys and call it before exiting. And basically debug all possible problems before exiting and the exit safely. – user2530266 Jul 07 '14 at 12:45
  • It is up to __you__ to decide how __your__ application can be closed in a proper way. How would __we__ know what clean-up code it advisable..? As for a HotKey, consider using Alt-F4! – TaW Jul 07 '14 at 17:49

0 Answers0