Console application freezes upon mouse click

I have a very simple console application in C # that displays text and loops awaiting input until an escape key or a wait period is pressed.

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 application with the mouse (for example, to get focus), all the activity on the screen freezes until I right-click or release the escape key. During this time, the console title also changes to "Select AppName", assuming it "AppName"was a title earlier.

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

, , . ? , ? , .

+3
1

.

, - Mark and Paste. (Alt + Space, Edit, Mark/Paste). , .

- (Alt + Space, Defaults, Options, Edit Options, Quick Edit Mode). , .

+12

All Articles