38

I have a console program that has different components that run like this:

void start() {
while(true){
     DoSomething();
     Thread.Sleep(1000*5);
}
}

My main entry point looks like [pseudo-ish code]

Thread.Start(Componenet1.Start);
Thread.Start(Componenet2.Start);

while(true){
     Console.Writeline("running");
     Thread.Sleep(1000*5);
}

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.

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.

Also note that sometimes the CPU can really be running at 100% on the machines so maybe thread priority is an issue?

SOLUTION: You need to disable quick edit mode. Here is working C# code to do this:

 // 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);
3
  • try to comment DoSomething(); and see if it behaves the same
    – Andrey
    Dec 15, 2010 at 19:04
  • The answer in c# code does not seem to work. The below accepted answer does.
    – liang
    Jan 17, 2017 at 10:43
  • 1
    The C# code does not work for me. A working solution can be found here: stackoverflow.com/a/36720802/1999917
    – Petter T
    Oct 10, 2017 at 12:47

1 Answer 1

51

If the user accidentally clicks into the black console window, the cursor changes to a filled white rectangle, and the app hangs at the next Console.Write statement, until another clic is made.

It is a generic feature of the Console window when its "QuickEdit Mode" is enabled.

In order to disable that feature, you should uncheck the "QuickEdit Mode" option of your app's console window at run-time.

5
  • aaaaaaah, i think I noticed the cursor changing. I will try to confirm this when it next happens. Dec 15, 2010 at 19:05
  • @nextgenneo : If it's that, you can disable it programmatically : bytes.com/topic/visual-basic-net/answers/… Dec 15, 2010 at 19:07
  • yeah, great. confirmed this was the problem! Dec 15, 2010 at 19:08
  • @nextgenneo : Added a link in previous comment :) Dec 15, 2010 at 19:09
  • 2
    is there a way to turn off it by default ?
    – Arti
    Apr 8, 2019 at 11:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.