Console application freezes on mouse click
Asked Answered
F

2

5

I have a very simple C# console application that displays some text and loops waiting for input until the escape key is pressed or the timeout period is served.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

namespace SampleApp
{
    public static class Program
    {
        public static void Main (string [] args)
        {
            var key = new ConsoleKeyInfo();
            var watch = Stopwatch.StartNew();
            var timeout = TimeSpan.FromSeconds(5);

            Console.WriteLine("Press escape to return to the previous screen...");
            Console.WriteLine();

            do
            {
                Console.WriteLine("This screen will automatically close in " + ((timeout + TimeSpan.FromSeconds(1)) - watch.Elapsed).ToString(@"hh\:mm\:ss") + ".");

                if (Console.KeyAvailable) { key = Console.ReadKey(true); }
                else { Thread.Sleep(TimeSpan.FromSeconds(0.10D)); }
            }
           while ((key.Key != ConsoleKey.Escape) && (timeout > (watch.Elapsed - TimeSpan.FromSeconds(0.5D))));

            watch.Stop();
        }
    }
}

This works fine but if I click on the console app with the mouse (to gain focus for example), all activity on the screen freezes until I right click or press escape. During this time, the title of the console also changes to "Select AppName" assuming "AppName" was the title before.

If I right-click ion the console first, the do {...} while (); loop seems to go crazy and prints a lot of extra lines.

Since I am not aware of this behavior of the console, not sure what to ask. Is this to be expected? If so, can I change this behavior? If not, any suggestions for workarounds would be appreciated.

Flavorful answered 19/2, 2014 at 15:5 Comment(2)
Sounds to me that you've activated the console window's Mark and Paste commands somehow. Normally activate through the system menu (Alt + Space, Edit, Mark/Paste). That doesn't have anything to do with this code of course.Batton
Thank you @HansPassant. I would not have thought of that. Apparently, Quick Edit Mode was set in the console defaults (Alt + Space, Defaults, Options, Edit Options, Quick Edit Mode) for some reason. Unchecking that resolved the issue. You should post your comment as an answer.Flavorful
F
22

The issue was solved using Hans' comment above.

Sounds to me that you've activated the console window's Mark and Paste commands somehow. Normally activate through the system menu (Alt + Space, Edit, Mark/Paste). That doesn't have anything to do with this code of course.

Apparently, Quick Edit Mode was set in the console defaults (Alt + Space, Defaults, Options, Edit Options, Quick Edit Mode) for some reason. Unchecking that resolved the issue.

Flavorful answered 8/7, 2014 at 15:54 Comment(0)
R
3
//call this class to disable quick edit mode.

public static void Main()
{
//disable console quick edit mode
 DisableConsoleQuickEdit.Go();

}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;


static class DisableConsoleQuickEdit
{

    const uint ENABLE_QUICK_EDIT = 0x0040;

    // STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
    const int STD_INPUT_HANDLE = -10;

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport("kernel32.dll")]
    static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

    [DllImport("kernel32.dll")]
    static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

    internal static bool Go()
    {

        IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);

        // get current console mode
        uint consoleMode;
        if (!GetConsoleMode(consoleHandle, out consoleMode))
        {
            // ERROR: Unable to get console mode.
            return false;
        }

        // Clear the quick edit bit in the mode flags
        consoleMode &= ~ENABLE_QUICK_EDIT;

        // set the new mode
        if (!SetConsoleMode(consoleHandle, consoleMode))
        {
            // ERROR: Unable to set console mode
            return false;
        }

        return true;
    }
}
Reste answered 16/12, 2020 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.