From 914a58f2a206f8adc30f20dfad25c3fc7cb47493 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Wed, 31 Dec 2014 14:08:22 -0800 Subject: [PATCH] Cleanup resources after USB disconnect while Connected - Fixes #85321330 --- .../PrinterConnectionAndCommunication.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index 4bc7b4f32..939d72701 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -1922,11 +1922,14 @@ namespace MatterHackers.MatterControl.PrinterCommunication serialPort.Write(lineToWrite); //Debug.Write("w: " + lineToWrite); } - catch (IOException) + catch (IOException ex) { - OnConnectionFailed(null); + Trace.WriteLine("Error writing to printer: " + ex.Message); + + // Handle hardware disconnects by relaying the failure reason and shutting down open resources + AbortConnectionAttempt("Connection Lost - " + ex.Message); } - catch (TimeoutException) + catch (TimeoutException ex) { } } @@ -1977,6 +1980,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication /// Shutdown/join the readFromPrinterThread public void AbortConnectionAttempt(string abortReason, bool shutdownReadLoop = true) { + // Set .Disconnecting to allow the readloop to exit gracefully before a forced thread join (and extended timeout) CommunicationState = CommunicationStates.Disconnecting; // Shudown the connectionAttempt thread