title: How to create multiple threads in c#
body: <p>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.</p>
  
  <p>I used this code to get the number of ports in my machine..</p>
  
  <pre><code>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 + "\n");
      }
  </code></pre>
  
  <p>After reading the comments i am using this code but not getting messages.. what is the problem ?</p>
  
  <pre><code>public void AssignThreadsToPorts()
      {
          string[] ports = SerialPort.GetPortNames();
          foreach (string port in ports)
          {
              SerialPort sp = new SerialPort();
              sp.PortName = port;
              sp.Open();
  
              new Thread(() =&gt;
              {
                  if (sp.IsOpen)
                  {
                      string str = sp.ReadLine().ToString();
                      MessageBox.Show(str);
                  }           
              }).Start();
          } 
      } 
  </code></pre>