控制台特定键输入无阻塞

时间:2016-04-20 08:36:28

标签: c# console-application

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

以下是代码:

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

它会生成一个简单的游戏:

| :x|
| : |
|x: |
| :↑|

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

2 个答案:

答案 0 :(得分:1)

另一种可能的解决方法是使用BackgroundWorker来监听输入。这样,您可以在“相同”时间处理用户输入和主代码。它类似于一个单独的线程。

您需要在程序中添加using System.ComponentModel;

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 :(得分:0)

正如RB所述,您可以为按键设置一个监听器,如果是,则检查是否为真,如果他们将按键重置为空并将汽车向该方向移动

Listen for key press in .NET console app