控制台应用程序在鼠标单击时冻结 [英] Console application freezes on mouse click

查看:25
本文介绍了控制台应用程序在鼠标单击时冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的 C# 控制台应用程序,它显示一些文本和循环等待输入,直到按下转义键或提供超时时间.

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();
        }
    }
}

这很好用,但是如果我用鼠标单击控制台应用程序(例如获得焦点),屏幕上的所有活动都会冻结,直到我右键单击或按退出键.在此期间,控制台的标题也更改为 "Select AppName",假设 "AppName" 是之前的标题.

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.

如果我先右键单击控制台,do {...} while (); 循环似乎会发疯并打印出很多额外的行.

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.

推荐答案

使用上面 Hans 的评论解决了这个问题.

The issue was solved using Hans' comment above.

显然,出于某种原因,在控制台默认值(Alt + Space、默认值、选项、编辑选项、快速编辑模式)中设置了快速编辑模式.取消选中即可解决问题.

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.

这篇关于控制台应用程序在鼠标单击时冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆