public partial class CustomSerialPort : SerialPort { public CustomSerialPort(string port) : base(port) { } public new bool Open() { try { base.Open(); /* ** because of the issue with the FTDI USB serial device, ** the call to the stream's finalize is suppressed ** ** it will be un-suppressed in Dispose if the stream ** is still good */ GC.SuppressFinalize(BaseStream); } catch(Exception ex) { return false; } return true; } public new void Dispose() { Dispose(true); } protected override void Dispose(bool disposing) { if (disposing && (base.Container != null)) { base.Container.Dispose(); } try { var stream = (Stream)typeof(SerialPort).GetField("internalSerialStream", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this); if (stream != null) { try { stream.Dispose(); } catch (Exception ex) { } } /* ** because of the issue with the FTDI USB serial device, ** the call to the stream's finalize is suppressed ** ** an attempt to un-suppress the stream's finalize is made ** here, but if it fails, the exception is caught and ** ignored */ GC.ReRegisterForFinalize(BaseStream); } catch(Exception ex) { } base.Dispose(disposing); } public new bool Close() { try { base.Close(); } catch (Exception ex) { return false; } return true; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)