title: SerialPort.Open() throws IOException - Insufficient system resources exist to complete the requested service
body: <p>I have a .NET 4 Windows service I've written that periodically (usually once a day) communicates with an external device over a serial port. All in all the service works great, but for one customer, every now and then, a call to <code>SerialPort.Open()</code> throws the following exception:</p>
  
  <pre>System.IO.IOException: Insufficient system resources exist to complete the requested service.
  at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
  at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
  at System.IO.Ports.SerialPort.Open()
  </pre>
  
  <p>Based on the exception, would would think that the server is running low on resources, but that doesn't seem to be the case. The CPU is more or less idle and there's plenty of memory and disk.</p>
  
  <p>There are lots of mentions online of <code>SerialPort.Open()</code> throwing other IOExceptions and I have implemented <a href="http://zachsaw.blogspot.se/2010/07/net-serialport-woes.html" rel="noreferrer">Zach Saw's SerialPortFixer</a>, but it appears it fixes a different issue.</p>
  
  <p>Here's an example of what I'm doing (greatly simplified). A couple of instances of this class (using different serial port names) are in memory at all times and then the Run() method is called approximately once a day for each instance.</p>
  
  <pre><code>public class Collector
  {
      private SerialPort _port;
      private string _portName;
  
      public void Run()
      {
          try
          {
              // Run Zach Saw's IOException workaround
              SerialPortFixer.Execute(_portName);
  
              using (_port = new SerialPort(_portName, 9600, Parity.None, 8, StopBits.One))
              {
                  _port.DataReceived += PortDataReceived;
                  _port.ErrorReceived += PortErrorReceived;
                  _port.Handshake = Handshake.None;
                  _port.DtrEnable = true;
                  _port.RtsEnable = true;
  
                  _port.Open();
                  // Do the stuff
                  _port.Close();
              }
          }
          catch (Exception e)
          {
              // Handle exception
          }
      }
  
      private void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
      {
          // Do other stuff
      }
  
      private void PortErrorReceived(object sender, SerialErrorReceivedEventArgs e)
      {
          // Log error
      }
  }
  </code></pre>
  
  <p>Any help would be appreciated.</p>