如何在等待 ReadLine 时更新 C# Windows 控制台应用程序中的当前行?

发布于 2024-09-26 23:20:52 字数 343 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬 2024-10-03 23:20:53

是的。您可以在阻塞 Console.ReadLine 时从单独的线程写入控制台。

话虽如此,这会引起混乱。在您的情况下,您将清除用户在行中输入的内容(通过 Console.Clear()),并大幅移动光标位置。


编辑:这是一个显示这一点的示例:

namespace ConsoleApplication1
{
    using System;
    using System.Threading;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting");

            ThreadPool.QueueUserWorkItem(
                cb =>
                    {
                        int i = 1;
                        while (true)
                        {
                            Console.WriteLine("Background {0}", i++);
                            Thread.Sleep(1000);
                        }
                    });
            Console.WriteLine("Blocking");
            Console.WriteLine("Press Enter to exit...");
            Console.ReadLine();
        }
    }
}

如果运行此命令,您将看到控制台在 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:

namespace ConsoleApplication1
{
    using System;
    using System.Threading;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting");

            ThreadPool.QueueUserWorkItem(
                cb =>
                    {
                        int i = 1;
                        while (true)
                        {
                            Console.WriteLine("Background {0}", i++);
                            Thread.Sleep(1000);
                        }
                    });
            Console.WriteLine("Blocking");
            Console.WriteLine("Press Enter to exit...");
            Console.ReadLine();
        }
    }
}

If you run this, you'll see the Console waits on ReadLine, but the background thread still prints.

や莫失莫忘 2024-10-03 23:20:53

在循环内使用 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.

无可置疑 2024-10-03 23:20:53

希望这个解决方案可以帮助您:

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

namespace WaitForExit
{
    class Program
    {
        static void Main(string[] args)
        {
            new Thread(() =>
                {
                    Console.ReadLine();
                    Environment.Exit(0);
                }).Start();

            int i = 0;
            while (true)
            {
                Console.Clear();
                Console.WriteLine(++i);
                Thread.Sleep(1000);
            }
        }
    }
}    

May this solution helps you:

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

namespace WaitForExit
{
    class Program
    {
        static void Main(string[] args)
        {
            new Thread(() =>
                {
                    Console.ReadLine();
                    Environment.Exit(0);
                }).Start();

            int i = 0;
            while (true)
            {
                Console.Clear();
                Console.WriteLine(++i);
                Thread.Sleep(1000);
            }
        }
    }
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文