title: Thread doesn't read
body: <p>I'm writing C# code for use with Unity3D in order to read from a serial port. This is done with another thread. The reading thread hangs when calling the SerialPort.Read method. The reading thread class is as follows:</p>
  
  <pre><code>public class Serial_Port_Reader {
      private SerialPort port;
      private byte[] data;
      private int bytes;
  
      public Serial_Port_Reader(SerialPort in_port, int in_bytes) {
          port = in_port;
          bytes = in_bytes;
          data = new byte[bytes];
      }
      public void read() {
          try {
              while(true) {
                  IO.log.Add("A");
                  data = new byte[bytes];
                  IO.log.Add("C");
                  port.Read(data, 0, 1);
                  IO.log.Add("Data Received: "+DateTime.Now.ToString());
                  Data.parse(data);
                  IO.log.Add("D");
                  port.DiscardInBuffer();
              }
          }
          catch(ThreadAbortException e) {IO.log.Add("Abort:"+e.Message);}
          catch(IOException e) {IO.log.Add("IO:"+e.Message);}
          catch(Exception e) {IO.log.Add(e.Message);}
          finally {if(port.IsOpen) port.Close();}
      }
  }
  </code></pre>
  
  <p>It is called like this:</p>
  
  <pre><code>workaround = new Serial_Port_Reader(input, Data.bytes);
  reader = new Thread(new ThreadStart(workaround.read));
  reader.Start();
  </code></pre>
  
  <p>If the thread is already active it halts at "C" and remains there even when I send data. If I start the thread in the middle of receiving data, it will work as it should (i.e. it will read one character) and then halt at "C" on the next loop through.</p>
  
  <p>I am trying to get it to continuously read in data. Why does it stop at "C" and not continue under ordinary circumstances?</p>