From 717ac8d19bc410ac61389eb53b4ca5cf5bb56082 Mon Sep 17 00:00:00 2001 From: "Sean D. Cline" Date: Thu, 29 Apr 2021 13:27:32 -0400 Subject: [PATCH] Squash exceptions when recovering from a disconnected COM port. --- adrilight/Util/SerialStream.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/adrilight/Util/SerialStream.cs b/adrilight/Util/SerialStream.cs index 370d6ec..10d9bcf 100644 --- a/adrilight/Util/SerialStream.cs +++ b/adrilight/Util/SerialStream.cs @@ -247,11 +247,18 @@ private void DoWork(object tokenObject) } //to be safe, we reset the serial port - if (serialPort != null && serialPort.IsOpen) + try { - serialPort.Close(); + if (serialPort != null && serialPort.IsOpen) + { + serialPort.Close(); + } + serialPort?.Dispose(); + } + catch (Exception) + { + // possible IoException when device is unplugged } - serialPort?.Dispose(); serialPort = null; //allow the system some time to recover