如何在等待 ReadLine 时更新 C# Windows 控制台应用程序中的当前行?
在 C# 中构建 Windows 控制台应用程序时,是否可以在等待读取行时更新控制台中的行?
我当前的代码是:
do
{
Console.Clear();
Console.WriteLine("RA: " + scope.RightAscension);
Console.WriteLine("Dec: " + scope.Declination);
Console.WriteLine("Status: " + scope.Slewing);
System.Threading.Thread.Sleep(1000);
} while (true);
When building a Windows Console App in C#, is it possible to update lines in the console while waiting for a readline?
My current code is:
do
{
Console.Clear();
Console.WriteLine("RA: " + scope.RightAscension);
Console.WriteLine("Dec: " + scope.Declination);
Console.WriteLine("Status: " + scope.Slewing);
System.Threading.Thread.Sleep(1000);
} while (true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。您可以在阻塞 Console.ReadLine 时从单独的线程写入控制台。
话虽如此,这会引起混乱。在您的情况下,您将清除用户在行中输入的内容(通过 Console.Clear()),并大幅移动光标位置。
编辑:这是一个显示这一点的示例:
如果运行此命令,您将看到控制台在 ReadLine 上等待,但后台线程仍在打印。
Yes. You can write to the Console from a separate thread while blocking on Console.ReadLine.
That being said, it's going to cause confusion. In your case, you'll clear out what the user is typing half-way through their line (via Console.Clear()), plus move the cursor position around dramatically.
Edit: Here's an example that shows this:
If you run this, you'll see the Console waits on ReadLine, but the background thread still prints.
在循环内使用 Console.KeyAvailable。一旦返回 true,用户就开始输入,因此调用 ReadLine()。但它并没有形成一个非常有吸引力的用户界面。考虑 Windows 窗体。
Use Console.KeyAvailable inside the loop. As soon as it returns true, the user started typing so call ReadLine(). It doesn't make for a very attractive user interface though. Consider Windows Forms.
希望这个解决方案可以帮助您:
May this solution helps you: