Saturday, October 30, 2010

Console.Writeline() Hangs if User clicks in Console Window

The Problem

We had this issue with some .NET Console Applications at work, although I imagine the framework of the application doesn't really matter. The scenario is that you have a console app which needs to run in the background for a long time, in our case a FileCreate watcher script. But someone is administering the server or machine, and accidentally clicks somewhere on the console window. This causes the title of the window to change from "C:\Windows\system32\cmd.exe" or something like that to "SELECT - C:\Windows\system32\cmd.exe". Now the application will halt its execution the next time a Console.Write() or Console.WriteLine() occurs in the application.

The Solution

This problem is actually a feature of the console window in Windows XP, called "QuickEdit." Normally, you have to go to the menu at top-left before you can select something in the console window for copying, but with QuickEdit on you can do this at any time and are in danger of halting your application. To turn it off:

  1. Goto the menu by clicking icon in top-left corner of console window.
  2. Goto "Properties"
  3. On the Options tab, under "Edit Options," un-check "QuickEdit Mode"
  4. Click "OK" and confirm that you would like to apply changes to all windows with same title.

Apparently there is a way to adjust this setting from your application, but I didn't really need to since I was only worried about this on one server. Also, the instructions are in VB.NET, eww.

Disqus for A Nofsinger's Blog