首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无阻塞的控制台特定按键输入

无阻塞的控制台特定按键输入
EN

Stack Overflow用户
提问于 2016-04-20 16:36:28
回答 2查看 3.5K关注 0票数 2

我需要在不阻塞循环的情况下在控制台应用程序中输入特定的键(arrows.left和arrows.right)。

代码如下:

代码语言:javascript
复制
while (fuel>0) {
    moveAndGenerate();

    for (int i=0;i<road.GetLength(0); i++)
    {
        for (int j = 0; j < road.GetLength(1); j++)
        {
            Console.Write(string.Format("{0} ", road[i, j]));
        }
        Console.Write(Environment.NewLine + Environment.NewLine);
    }
    Console.WriteLine("Paliwo: "+ (fuel=fuel-5) + "%");

    moveAndGenerate();
    replaceArrays();            

    Thread.Sleep(1000);
    Console.Clear();
}

它生成一个简单的游戏:

代码语言:javascript
复制
| :x|
| : |
|x: |
| :↑|

只要有燃料就在循环内。我希望箭头向右/向左移动,而不等待Console.ReadKey()。有可能吗?

EN

回答 2

Stack Overflow用户

发布于 2016-04-20 16:45:00

Stack Overflow用户

发布于 2017-07-14 14:27:24

另一种可能的解决方法是使用BackgroundWorker侦听输入。这样你就可以同时处理用户输入和主代码了。它类似于一个单独的线程。

您需要将using System.ComponentModel;添加到您的程序中。

代码语言:javascript
复制
static BackgroundWorker backgroundWorker1 = new BackgroundWorker(); // Create the background worker
static string input = ""; // where the user command is stored

public static void Main()
{
    // All the code preceding the main while loop is here
    // Variable declarations etc.


    //Setup a background worker
    backgroundWorker1.DoWork += BackgroundWorker1_DoWork; // This tells the worker what to do once it starts working
    backgroundWorker1.RunWorkerCompleted += BackgroundWorker1_RunWorkerCompleted;  // This tells the worker what to do once its task is completed

    backgroundWorker1.RunWorkerAsync(); // This starts the background worker

    // Your main loop
    while (fuel>0) 
    {
        moveAndGenerate();

        for (int i=0;i<road.GetLength(0); i++)
        {
            for (int j = 0; j < road.GetLength(1); j++)
            {
                Console.Write(string.Format("{0} ", road[i, j]));
            }
            Console.Write(Environment.NewLine + Environment.NewLine);
        }
        Console.WriteLine("Paliwo: "+ (fuel=fuel-5) + "%");

        moveAndGenerate();
        replaceArrays();            

        Thread.Sleep(1000);
        Console.Clear();
    }

    // This is what the background worker will do in the background
    private static void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        if (Console.KeyAvailable == false)
        {
            System.Threading.Thread.Sleep(100); // prevent the thread from eating too much CPU time
        }
        else
        {
            input = Console.In.ReadLine();

            // Do stuff with input here or, since you can make it a static
            // variable, do stuff with it in the while loop.
        }
    }

    // This is what will happen when the worker completes reading
    // a user input; since its task was completed, it will need to restart
    private static void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs 
    {
        if(!backgroundWorker1.IsBusy)
        {
            backgroundWorker1.RunWorkerAsync(); // restart the worker
        }

    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36738199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档