<legend id='Vidi4'><style id='Vidi4'><dir id='Vidi4'><q id='Vidi4'></q></dir></style></legend>

        <tfoot id='Vidi4'></tfoot>
      1. <small id='Vidi4'></small><noframes id='Vidi4'>

          <bdo id='Vidi4'></bdo><ul id='Vidi4'></ul>

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

        如何在c#中创建多个线程

        时间:2023-11-10
            • <bdo id='hxVCu'></bdo><ul id='hxVCu'></ul>

                <tbody id='hxVCu'></tbody>
              <legend id='hxVCu'><style id='hxVCu'><dir id='hxVCu'><q id='hxVCu'></q></dir></style></legend>
            • <small id='hxVCu'></small><noframes id='hxVCu'>

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

                  本文介绍了如何在c#中创建多个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要监听我机器上的所有串口.假设我的机器有 4 个串口,我必须创建 4 个线程并开始分别用附加的线程监听每个端口.

                  I am need to listen to all the serial ports in my machine. Say if my machine has 4 serial ports, i have to create 4 threads and start listening to each port with the attached thread respectively.

                  我使用这段代码来获取我机器中的端口数..

                  I used this code to get the number of ports in my machine..

                  private SerialPort comPort = new SerialPort();
                  
                      public void GetAllPortNamesAvailable()
                      {
                          string[] ports = SerialPort.GetPortNames();
                          foreach (string port in ports)
                          {
                              //How to start a thread here ??
                          }
                      }
                  
                      public void AssignThreadtoPort()
                      {
                          string msg = comPort.ReadLine();
                          this.GetMessageRichTextBox("Message : " + msg + "
                  ");
                      }
                  

                  阅读评论后,我正在使用此代码但没有收到消息.. 有什么问题?

                  After reading the comments i am using this code but not getting messages.. what is the problem ?

                  public void AssignThreadsToPorts()
                      {
                          string[] ports = SerialPort.GetPortNames();
                          foreach (string port in ports)
                          {
                              SerialPort sp = new SerialPort();
                              sp.PortName = port;
                              sp.Open();
                  
                              new Thread(() =>
                              {
                                  if (sp.IsOpen)
                                  {
                                      string str = sp.ReadLine().ToString();
                                      MessageBox.Show(str);
                                  }           
                              }).Start();
                          } 
                      } 
                  

                  推荐答案

                  你可以使用 线程池:

                  string[] ports = SerialPort.GetPortNames();
                  foreach (string port in ports)
                  {
                      ThreadPool.QueueUserWorkItem(state =>
                      {
                          // This will execute in a new thread
                      });
                  }
                  

                  或手动创建并启动 线程:

                  string[] ports = SerialPort.GetPortNames();
                  foreach (string port in ports)
                  {
                      new Thread(() => 
                      {
                          // This will execute in a new thread
                      }).Start();
                  }
                  

                  这篇关于如何在c#中创建多个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:.Net SerialPort Readline 与 DataReceived 事件处理程序 下一篇:获取 USB 适配器的 com 端口号

                  相关文章

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

                  <tfoot id='uSKo2'></tfoot><legend id='uSKo2'><style id='uSKo2'><dir id='uSKo2'><q id='uSKo2'></q></dir></style></legend>

                    <bdo id='uSKo2'></bdo><ul id='uSKo2'></ul>

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