我需要在不阻塞循环的情况下在控制台应用程序中输入特定的键(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()。有可能吗?
发布于 2016-04-20 16:45:00
发布于 2017-07-14 14:27:24
另一种可能的解决方法是使用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
}
}
}https://stackoverflow.com/questions/36738199
复制相似问题