【问题标题】:C# Console.ReadKey 1 character lag?C# Console.ReadKey 1 个字符滞后?
【发布时间】:2012-07-25 05:47:18
【问题描述】:

我正在尝试使用 Console.ReadKey() 函数来拦截用户的击键并重建他们在屏幕上输入的内容(因为我需要经常清除屏幕,经常移动光标,这似乎是确保他们输入的内容不会消失或出现在屏幕各处的随机点的最全面的方法。

我的问题是:有没有其他人在做类似的事情时经历过 1 个字符的“滞后”,因为没有更好的术语?假设我想输入单词“This”。当我按“T”时,无论我等待多长时间,什么都没有出现。当我按“h”时,会出现“T”。 “i”,出现“h”。在我按下另一个键之前,我键入的字母不会出现,即使那个键是空格键。有人对我做错了什么有任何建议吗?我确定这与我使用 Console.Readkey 的方式有关,我只是看不出有什么替代方案可行。我在下面附上了一个小而简单的例子。

谢谢!

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

    namespace ConsoleApplication2

{
    class Program
    {

        private static string userInput = "";
        static ConsoleKeyInfo inf;
        static StringBuilder input = new StringBuilder();

        static void Main(string[] args)
        {
            Thread tickThread = new Thread(new ThreadStart(DrawScreen));
            Thread userThread = new Thread(new ThreadStart(UserEventHandler));
            tickThread.Start();
            Thread.Sleep(1);
            userThread.Start();
            Thread.Sleep(20000);
            tickThread.Abort();
            userThread.Abort();
        }


        private static void DrawScreen()
        {
            while (true)
            {
                Console.Clear();
                Console.SetCursorPosition(0, 0);
                Console.Write("> " + userInput);
                Thread.Sleep(300);
            }
        }


        private static void UserEventHandler()
        {
            inf = Console.ReadKey(true);

            while (true)
            {
                if (inf.Key != ConsoleKey.Enter)
                    input.Append(inf.KeyChar);
                else
                {
                    input = new StringBuilder();
                    userInput = "";
                }

                inf = Console.ReadKey(true);

                userInput = input.ToString();

            }
        }

    }
}

【问题讨论】:

  • 我很抱歉-我只是从我的主程序中复制了这一点,以提供一个简单的示例。它有一个目的是我的主要程序,它在这里没有任何作用。我将从这个例子中删除它。
  • 也许见KeyAvailable .. 或许能够完全消除线程?
  • 这是一团糟。请澄清。你想做什么?为什么你不能使用 readline,并在点击输入之前获取所有类型?
  • 在我的主程序中,这只是我用来学习 C# 的一个蹩脚的小游戏,我希望控制台分成 2 个部分:屏幕顶部和中间的所有部分都是地图/ hud/无论玩家。它会不断令人耳目一新。这意味着每 1.5 秒左右,我将清除屏幕或使用 Console.SetCursorPosition 将光标移动到屏幕的不同部分。屏幕底部是为用户键入的内容保留的。然而,这种设置会不断清除用户正在输入的内容,否则他们的字符会出现在屏幕上的风险。
  • 我昨天问了一个问题,并被告知我最好的选择是“使用 Console.ReadKey 重建用户的输入”。因此,我正在做的这一点点。此示例仅关注该目标。 stackoverflow.com/questions/11628432/…

标签: c# console.readkey


【解决方案1】:

因为你有2次Console.ReadKey()

如果你把你的代码改成这个

    private static void UserEventHandler()
    {
        while (true)
        {
            inf = Console.ReadKey(true);
            if (inf.Key != ConsoleKey.Enter)
                input.Append(inf.KeyChar);
            else
            {
                input = new StringBuilder();
                userInput = "";
            }
            userInput = input.ToString();
        }
    }

它不会滞后。第二个Console.ReadKey() 阻塞在您的代码中。我没有检查你是否需要readkey的参数true,这是你自己找出来的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2013-06-16
    • 2021-06-15
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多