• <legend id='opwvI'><style id='opwvI'><dir id='opwvI'><q id='opwvI'></q></dir></style></legend>
      <bdo id='opwvI'></bdo><ul id='opwvI'></ul>
    <i id='opwvI'><tr id='opwvI'><dt id='opwvI'><q id='opwvI'><span id='opwvI'><b id='opwvI'><form id='opwvI'><ins id='opwvI'></ins><ul id='opwvI'></ul><sub id='opwvI'></sub></form><legend id='opwvI'></legend><bdo id='opwvI'><pre id='opwvI'><center id='opwvI'></center></pre></bdo></b><th id='opwvI'></th></span></q></dt></tr></i><div id='opwvI'><tfoot id='opwvI'></tfoot><dl id='opwvI'><fieldset id='opwvI'></fieldset></dl></div>
  • <small id='opwvI'></small><noframes id='opwvI'>

    <tfoot id='opwvI'></tfoot>

        Console.WriteLine 是否阻塞?

        时间:2023-08-27
            <tbody id='BlMmq'></tbody>

          <small id='BlMmq'></small><noframes id='BlMmq'>

            <bdo id='BlMmq'></bdo><ul id='BlMmq'></ul>
            1. <tfoot id='BlMmq'></tfoot>

              <i id='BlMmq'><tr id='BlMmq'><dt id='BlMmq'><q id='BlMmq'><span id='BlMmq'><b id='BlMmq'><form id='BlMmq'><ins id='BlMmq'></ins><ul id='BlMmq'></ul><sub id='BlMmq'></sub></form><legend id='BlMmq'></legend><bdo id='BlMmq'><pre id='BlMmq'><center id='BlMmq'></center></pre></bdo></b><th id='BlMmq'></th></span></q></dt></tr></i><div id='BlMmq'><tfoot id='BlMmq'></tfoot><dl id='BlMmq'><fieldset id='BlMmq'></fieldset></dl></div>
              <legend id='BlMmq'><style id='BlMmq'><dir id='BlMmq'><q id='BlMmq'></q></dir></style></legend>

                1. 本文介绍了Console.WriteLine 是否阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  Console.WriteLine 会阻塞直到输出被写入还是立即返回?

                  Does Console.WriteLine block until the output has been written or does it return immediately?

                  如果确实阻塞了,是否有将异步输出写入控制台的方法?

                  If it does block is there a method of writing asynchronous output to the Console?

                  推荐答案

                  Console.WriteLine 是否阻塞直到输出已被写入或已完成立即返回?

                  Does Console.WriteLine block until the output has been written or does it return immediately?

                  是的.

                  如果它确实阻塞,是否有一种方法将异步输出写入控制台?

                  If it does block is there a method of writing asynchronous output to the Console?

                  如果您使用的是 .NET 4.0,那么在不阻塞的情况下写入控制台的解决方案非常简单.这个想法是将文本值排队并让一个专用线程执行 Console.WriteLine 调用.生产者-消费者模式在这里是理想的,因为它保留了使用原生 Console 类时隐含的时间顺序..NET 4.0 使这变得简单的原因是因为它具有 BlockingCollection促进生产者-消费者模式生产的类.如果您没有使用 .NET 4.0,那么您可以通过下载 Reactive Extensions 来获得反向移植 框架.

                  The solution to writing to the console without blocking is surprisingly trivial if you are using .NET 4.0. The idea is to queue up the text values and let a single dedicated thread do the Console.WriteLine calls. The producer-consumer pattern is ideal here because it preserves the temporal ordering that is implicit when using the native Console class. The reason why .NET 4.0 makes this easy is because it has the BlockingCollection class which facilitates the production of a producer-consumer pattern. If you are not using .NET 4.0 then you can get a backport by downloading the Reactive Extensions framework.

                  public static class NonBlockingConsole
                  {
                    private static BlockingCollection<string> m_Queue = new BlockingCollection<string>();
                  
                    static NonBlockingConsole()
                    {
                      var thread = new Thread(
                        () =>
                        {
                          while (true) Console.WriteLine(m_Queue.Take());
                        });
                      thread.IsBackground = true;
                      thread.Start();
                    }
                  
                    public static void WriteLine(string value)
                    {
                      m_Queue.Add(value);
                    }
                  }
                  

                  这篇关于Console.WriteLine 是否阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:编写控制台应用程序时,有没有办法创建第二个 下一篇:来自控制台项目的 WPF 窗口?

                  相关文章

                  最新文章

                  <legend id='zYFuh'><style id='zYFuh'><dir id='zYFuh'><q id='zYFuh'></q></dir></style></legend>
                2. <i id='zYFuh'><tr id='zYFuh'><dt id='zYFuh'><q id='zYFuh'><span id='zYFuh'><b id='zYFuh'><form id='zYFuh'><ins id='zYFuh'></ins><ul id='zYFuh'></ul><sub id='zYFuh'></sub></form><legend id='zYFuh'></legend><bdo id='zYFuh'><pre id='zYFuh'><center id='zYFuh'></center></pre></bdo></b><th id='zYFuh'></th></span></q></dt></tr></i><div id='zYFuh'><tfoot id='zYFuh'></tfoot><dl id='zYFuh'><fieldset id='zYFuh'></fieldset></dl></div>

                    • <bdo id='zYFuh'></bdo><ul id='zYFuh'></ul>
                    1. <tfoot id='zYFuh'></tfoot>
                    2. <small id='zYFuh'></small><noframes id='zYFuh'>