title: problem with two .NET threads and hardware access
body: <p>I'm creating an application which communicates with the device via FT2232H USB/RS232 converter. For communication I'm using FTD2XX_NET.dll library from FTDI website.
  <br>
  I'm using two threads:
  <li> first thread continuously reads data from the device
  <li> the second thread is the main thread of the Windows Form Application
  <br><br>
  I've got a problem when I'm trying to write any data to the device while the receiver's thread is running. The main thread simply hangs up on ftdiDevice.Write function.
  <br><br>
  I tried to synchronize both threads so that only one thread can use Read/Write function at the same time, but it didn't help.
  <br><br>
  Below code responsible for the communication. Note that following functions are methods of FtdiPort class.
  <br><br></p>
  
  <h2>Receiver's thread</h2>
  
  <pre><code>
          private void receiverLoop()
          {
              if (this.DataReceivedHandler == null)
              {
                  throw new BackendException("dataReceived delegate is not set");
              }
  
              FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
              byte[] readBytes = new byte[this.ReadBufferSize];
  
              while (true)
              {
                  lock (FtdiPort.threadLocker)
                  {
                      UInt32 numBytesRead = 0;
  
                      ftStatus = ftdiDevice.Read(readBytes, this.ReadBufferSize, ref numBytesRead);
  
                      if (ftStatus == FTDI.FT_STATUS.FT_OK)
                      {
                          this.DataReceivedHandler(readBytes, numBytesRead);
                      }
                      else
                      {
                          Trace.WriteLine(String.Format("Couldn't read data from ftdi: status {0}", ftStatus));
                          Thread.Sleep(10);
                      }                    
                  }
                  Thread.Sleep(this.RXThreadDelay);
  
              }
          }
  </code></pre>
  
  <p><br></p>
  
  <h2>Write function called from main thread</h2>
  
  <p><pre><code></p>
  
  <pre><code>    public void Write(byte[] data, int length)
      {
          if (this.IsOpened)
          {
              uint i = 0;
  
              lock (FtdiPort.threadLocker)
              {
                  this.ftdiDevice.Write(data, length, ref i);
              }
  
              Thread.Sleep(1);
              if (i != (int)length)
              {
                  throw new BackendException("Couldnt send all data");
              }
          }
          else
          {
              throw new BackendException("Port is closed");
          }
      }
  </code></pre>
  
  <p></pre></code>
  <br></p>
  
  <h2>Object used to synchronize two threads</h2>
  
  <p><pre><code>
  static Object threadLocker = new Object();
  </pre></code></p>
  
  <h2>Method that starts the receiver's thread</h2>
  
  <p><pre><code>
          private void startReceiver()
          {
              if (this.DataReceivedHandler == null)
              {
                  return;
              }
              if (this.IsOpened == false)
              {
                  throw new BackendException("Trying to start listening for raw data while disconnected");
              }
              this.receiverThread = new Thread(this.receiverLoop);
              //this.receiverThread.Name = "protocolListener";
              this.receiverThread.IsBackground = true;
              this.receiverThread.Start();
          }
  </pre></code>
  The ftdiDevice.Write function doesn't hang up if I comment following line:</p>
  
  <pre><code>ftStatus = ftdiDevice.Read(readBytes, this.ReadBufferSize, ref numBytesRead);</code></pre>