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

    1. <tfoot id='zFo7H'></tfoot>

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

        c#在不停止应用程序的情况下读取用户输入

        时间:2023-08-27

        <small id='5wUmc'></small><noframes id='5wUmc'>

          <legend id='5wUmc'><style id='5wUmc'><dir id='5wUmc'><q id='5wUmc'></q></dir></style></legend>

                <bdo id='5wUmc'></bdo><ul id='5wUmc'></ul>
                1. <i id='5wUmc'><tr id='5wUmc'><dt id='5wUmc'><q id='5wUmc'><span id='5wUmc'><b id='5wUmc'><form id='5wUmc'><ins id='5wUmc'></ins><ul id='5wUmc'></ul><sub id='5wUmc'></sub></form><legend id='5wUmc'></legend><bdo id='5wUmc'><pre id='5wUmc'><center id='5wUmc'></center></pre></bdo></b><th id='5wUmc'></th></span></q></dt></tr></i><div id='5wUmc'><tfoot id='5wUmc'></tfoot><dl id='5wUmc'><fieldset id='5wUmc'></fieldset></dl></div>
                    <tbody id='5wUmc'></tbody>
                  <tfoot id='5wUmc'></tfoot>
                  本文介绍了c#在不停止应用程序的情况下读取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述


                  我知道我可以为此使用 ReadKey,但它会冻结应用程序,直到用户按下一个键.是否有可能(在控制台应用程序中)运行一些循环并且仍然能够做出反应?我只能想到事件,但不确定如何在控制台中使用它们.我的想法是循环会在每次迭代期间检查输入.


                  I know I can use ReadKey for that but it will freeze the app until user presses a key. Is it possible (in console app) to have some loop running and still be able to react? I can only think of events but not sure how to use them in console. My idea was that the loop would check for input during each iteration.

                  推荐答案

                  我为自己的应用程序这样做的方法是有一个专用线程调用 System.Console.ReadKey(true) 并将按下的键(和任何其他事件)放入消息队列中.

                  They way I have done this for my own application was to have a dedicated thread that calls into System.Console.ReadKey(true) and puts the keys pressed (and any other events) into a message queue.

                  然后主线程在一个循环中为这个队列提供服务(以类似于 Win32 应用程序中的主循环的方式),确保呈现和事件处理都在一个线程上处理.

                  The main thread then services this queue in a loop (in a similar fashion to the main loop in a Win32 application), ensuring that rendering and event processing is all handled on a single thread.

                  private void StartKeyboardListener()
                  {
                      var thread = new Thread(() => {
                                                        while (!this.stopping)
                                                        {
                                                            ConsoleKeyInfo key = System.Console.ReadKey(true);
                                                            this.messageQueue.Enqueue(new KeyboardMessage(key));
                                                        }
                                                    });
                  
                      thread.IsBackground = true;
                      thread.Start();
                  }
                  
                  private void MessageLoop()
                  {
                      while (!this.stopping)
                      {
                          Message message = this.messageQueue.Dequeue(DEQUEUE_TIMEOUT);
                  
                          if (message != null)
                          {
                              switch (message.MessageType)
                              {
                                  case MessageType.Keyboard:
                                      HandleKeyboardMessage((KeyboardMessage) message);
                                      break;
                                  ...
                              }
                          }
                  
                          Thread.Yield(); // or Thread.Sleep(0)
                      }
                  }
                  

                  这篇关于c#在不停止应用程序的情况下读取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:不启动控制台的 .Net 控制台应用程序 下一篇:在控制台应用程序中禁用用户输入

                  相关文章

                  最新文章

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

                  <small id='8ATYM'></small><noframes id='8ATYM'>

                    <bdo id='8ATYM'></bdo><ul id='8ATYM'></ul>

                4. <tfoot id='8ATYM'></tfoot>