title: Windows Console Application Getting Stuck (Needs Key Press)
body: <p>I have a console program that has different components that run like this:  </p>
  
  <pre><code>void start() {
  while(true){
       DoSomething();
       Thread.Sleep(1000*5);
  }
  }
  </code></pre>
  
  <p>My main entry point looks like [pseudo-ish code]</p>
  
  <pre><code>Thread.Start(Componenet1.Start);
  Thread.Start(Componenet2.Start);
  
  while(true){
       Console.Writeline("running");
       Thread.Sleep(1000*5);
  }
  </code></pre>
  
  <p>There are no Console.Reads anywhere. My problem is SOMETIMES the application will be running great but then stop and if I press any key on the window it will start working again. This happens fairly infrequently but I have this program deployed on 100+ VM's running 24/7 in an automated environment.</p>
  
  <p>Also on the computer I have some AHK scripts and other stuff that manipulate the mouse but not sure if that has anything to do with it.</p>
  
  <p>Also note that sometimes the CPU can really be running at 100% on the machines so maybe thread priority is an issue?</p>
  
  <p><strong>SOLUTION</strong>: You need to disable quick edit mode. Here is working C# code to do this:</p>
  
  <pre><code> // http://msdn.microsoft.com/en-us/library/ms686033(VS.85).aspx
      [DllImport("kernel32.dll")]
      public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
  
      private const uint ENABLE_EXTENDED_FLAGS = 0x0080;
  
      static void Main(string[] args)
      {
           IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
           SetConsoleMode(handle, ENABLE_EXTENDED_FLAGS);
  </code></pre>