title: "The I/O operation has been aborted because of either a thread exit or an application request" on disconnecting serial
body: <p>I am writing a program to read data from serial and display it. Sometimes (not every time) it crashes when I disconnect serial with the exception <code>The I/O operation has been aborted because of either a thread exit or an application request</code>. (I guess something is wrong here even it happens not every time).</p>
  
  <p>Here is how I am reading serial:</p>
  
  <pre><code>private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
      // this line below is where the exception is
      string read = _serialPort.ReadLine().Replace(".", ",").Split('\r')[0];
  }
  
  // clicking on a button opens/closes serial
  private void button1_Click(object sender, EventArgs e)
  {
      if (isSerialConnected)
          disconnectSerial();
      else
          connectSerial();
  }
  
  public void connectSerial()
  {
      _serialPort.PortName = serialCombobox.SelectedItem.ToString();
      _serialPort.BaudRate = 9600;
      _serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
      _serialPort.Open();
  
      serialCombobox.Enabled = false;
      connectSerialButton.Text = "disconnect";
  
      isSerialConnected = true;
  }
  
  public void disconnectSerial()
  {
     _serialPort.Close();
  
      serialCombobox.Enabled = true;
      connectSerialButton.Text = "connect";
  
      isSerialConnected = false;
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>