微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当键入时打印行时,输入的文本被拉入

如何解决当键入时打印行时,输入的文本被拉入

我想知道是否有可能有一个标准的控制台行为,当输出碰巧打印时,输入行不会被拉到输出,同时仍然输入输入行。相反,我希望输入行保持它允许完成句子或 smth 的方式,而输出行仍然能够一个一个地打印。

这是我写的简单控制台来说明我的意思(C# .NET Core 控制台应用程序)

class Program
{
    static string LastMsg = "<empty>";
    
    static void Main(string[] args)
    {
        Console.WriteLine("Starting app..");

        new Thread(OutLoop).Start(); // Run secondary thread with sleep

        InLoop(); // Run main thread to read lines          
    }

    static void InLoop()
    {
        Console.WriteLine("In loop started");
        while(true)
        {
            LastMsg = Console.ReadLine();
            Console.WriteLine($"Message updated to = {LastMsg}");
        }
    }

    static void OutLoop()
    {
        Console.WriteLine("Out loop started");
        while(true)
        {
            Thread.Sleep(3000);
            Console.WriteLine($"Last message was = {LastMsg}");
        }
    }
}

以及程序的输出

Starting app..
In loop started
Out loop started
Last message was = <empty>
test    // there I managed to type the message before out message so it worked fine
Message updated to = test
Last message was = test
ttadfsdfLast message was = test // there the text was pulled and printed without pressing enter because of out console message while typing
Last message was = test
im writing somtLast message was = test // same here
Last message was = test
Last message was = test
Last message was = test

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。