title: SerialPort fires DataReceived event after close
body: <p>I'm experiencing a weird behavior while trying to stop a SerialPort: the DataReceived event continues to fire after unsubscribing and after calling <code>close</code>! (see <code>StopStreaming</code> in the following code). As a result, in my event handler code I get an InvalidOperationException with the message that "The port is closed".</p>
  
  <p>What am I missing? What is the correct way to close the port and stop the events?</p>
  
  <p>EDIT: I get this error every time I run my code. So this is not a race condition that happens randomly but rather a systematic problem indicating a completely broken code! However, I fail to see how...</p>
  
  <pre><code>private SerialPort comPort = new SerialPort();
  
  public override void StartStreaming()
  {
    comPort.Open();
    comPort.DiscardInBuffer();
    comPort.DataReceived += comPort_DataReceived;
  }
  
  public override void StopStreaming()
  {
    comPort.DataReceived -= comPort_DataReceived;
    comPort.Close();
    isStreaming = false;
  }
  
  private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
    if (e.EventType == SerialData.Chars)
    {
      SerialPort port = (SerialPort)sender;
      int N = comPort.BytesToRead;
      for (; N &gt; 0; N--)
      {
        byte b = Convert.ToByte(comPort.ReadByte());
        //... process b
      }
    }
  }
  </code></pre>
  
  <p>EDIT: following the suggestions, I changed <code>StopStreaming</code> code to something like this:</p>
  
  <pre><code>public override void StopStreaming()
  {
    comPort.DataReceived -= comPort_DataReceived;
    Thread.Sleep(1000);
    comPort.DiscardInBuffer();
    Thread.Sleep(1000);
    comPort.Close();
    isStreaming = false;
  }
  </code></pre>
  
  <p>It seems to work now but I'm not really that happy. I wish there was a more effective way to remove the callback rather than inserting sleep periods in the program.</p>