title: Read com ports using threading in c#
body: <p>I would like some advise on threading as I'm new to it. I've read several articles on threading online.</p>
  
  <p>I'm reading com ports data. I would like to use threading such that it will read the data every 5 seconds and update the data on the listbox. Currently, all the data is being read.
  I'm unsure where to start. </p>
  
  <p>Where shall i start putting my thread codes? I'm using Windows Form,c# VS2008.</p>
  
  <p>Here's my codes to read the data from com ports:</p>
  
  <pre><code>    void datareceived(object sender, SerialDataReceivedEventArgs e)
      {            
          myDelegate d = new myDelegate(update);
          listBox1.Invoke(d, new object[] { });
  
      }
  
  
      public void update()
      {           
  
          while (serialPortN.BytesToRead &gt; 0)
              bBuffer.Add((byte)serialPortN.ReadByte());
          ProcessBuffer(bBuffer);
  
      }
  
      private void ProcessBuffer(List&lt;byte&gt; bBuffer)
      {            
  
          int numberOfBytesToRead = 125;
  
          if (bBuffer.Count &gt;= numberOfBytesToRead)
          {            
  
  
                  listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + "  " + " HR: " + (bBuffer[103].ToString()));
  
  
              // Remove the bytes read from the list
              bBuffer.RemoveRange(0, numberOfBytesToRead);
  
          }
  
      }        
  </code></pre>
  
  <p>Thanks all!</p>