【问题标题】:Thread Sleep and ReadKey doesn't cooperate [duplicate]线程睡眠和 ReadKey 不合作[重复]
【发布时间】:2019-11-29 19:35:08
【问题描述】:

我想通过使用空格来停止整个 do-while 循环并写出我的 WriteLine,但它在按下空格键时没有任何作用。我认为这与 Thread.Sleep 有关,也许它在睡眠时不允许用户输入。如果有人能告诉我为什么这不起作用,我会很高兴

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

namespace casinov2
{
    class Program
    {
        static void Main(string[] args)
        {
            int x=3;
            int y = 15;
            int[] elemek = new int[7];
            do{
                while (!Console.KeyAvailable){
                    for (int i = 0; i < 5; i++){
                        Console.Clear();
                        Console.SetCursorPosition(x, y);
                        x++;
                        Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
                        System.Threading.Thread.Sleep(100);
                        if (i >= 4){
                            for (int j = 0; j < 5; j++){
                                Console.Clear();
                                Console.SetCursorPosition(x, y);
                                x--;
                                Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
                                System.Threading.Thread.Sleep(100);
                            }
                        i = -1;
                        }
                    }
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Spacebar);
            Console.WriteLine("ready");
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • i = -1; 阻止 for 循环完成。删除它。
  • 目前还不完全清楚您预期会发生什么。正如汉斯指出的那样,通过重置i,该循环将永远不会终止,因此您将永远不会到达您的代码尝试读取控制台输入的地步。另一方面,您似乎真的不希望循环终止。如果是这种情况,那么您需要独立于输出运行输入(即在每个标记重复的另一个线程中),或者您需要在循环内轮询,检查 Console.KeyAvailable 并在返回 @ 时退出循环987654325@.

标签: c#


【解决方案1】:

KeyAvailable 检查需要在 for 循环内:

static void Main(string[] args)
{
    int x=3;
    int y = 15;
    int[] elemek = new int[7];
    do{
        for (int i = 0; i < 5; i++){
            Console.Clear();
            Console.SetCursorPosition(x, y);
            x++;
            Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
            System.Threading.Thread.Sleep(100);
            if (i >= 4){
                for (int j = 0; j < 5; j++){
                    Console.Clear();
                    Console.SetCursorPosition(x, y);
                    x--;
                    Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
                    System.Threading.Thread.Sleep(100);
                }
                i = -1;
            }

            if (Console.KeyAvailable) break;
        }
    } while (Console.ReadKey(true).Key != ConsoleKey.Spacebar);
    Console.WriteLine("ready");
    Console.ReadLine();
}

【讨论】:

  • 第二个 while 内的循环工作正常,问题是为什么当我按空格时它没有打破整个 do-while
  • 因为在该块的末尾i 被重置为-1。我已经在本地进行了测试,并且已经为我解决了问题。
  • 在循环中,变量i 永远不会有5 的值,也不会有任何大于该值的值。按照您的建议更改 if 表达式将使该代码块完全没有意义。您认为这是一个错误的建议绝对是错误的。它仅通过完全避免有问题的代码来“修复”它。这不是真正的解决办法。 (不要被令人讨厌的 K&R 风格的支撑和糟糕的压痕误导……i = -1 位于内部 for 循环内。)
猜你喜欢
  • 2018-08-09
  • 2011-11-10
  • 2011-05-14
  • 2013-05-29
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
相关资源
最近更新 更多