Is SerialPort in .NET an unmanaged resource? Is my wrapped class right?

I have an adapter template (wrapper) for the serial port class. Should I implement the IDisposable template and call _wrappedSerialPort.Dispose () in it? There is my class, is that right?

public class SerialPortAdapter : ISerialPortAdapter { private bool _disposed; public event SerialDataReceivedEventHandler DataReceived; private readonly SerialPort _wrappedSerialPort; public SerialPort WrappedSerialPort { get { return _wrappedSerialPort; } } public string PortName { get { return _wrappedSerialPort.PortName; } set { _wrappedSerialPort.PortName = value; } } public BaudRate BaudRate { get { return (BaudRate)Enum.ToObject(typeof(BaudRate), _wrappedSerialPort.BaudRate); } set { _wrappedSerialPort.BaudRate = (int)value; } } public bool IsOpen { get { return WrappedSerialPort.IsOpen; } } public SerialPortAdapter(SerialPort serialPort) { _wrappedSerialPort = serialPort; _wrappedSerialPort.DataReceived += SerialPortDataReceived; } public void OpenPort() { if (!_disposed) { if (!WrappedSerialPort.IsOpen) { WrappedSerialPort.Open(); } } } public void ClosePort() { if (!_disposed) { if (WrappedSerialPort.IsOpen) { WrappedSerialPort.Close(); } } } public void WriteLine(string request) { ... } public void Write(byte[] request) { .... } public byte[] Read() { .... } public string ReadLine() { ... } private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) { if (DataReceived != null) { DataReceived(this, e); } } #region IDisposable Members public virtual void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Dispose managed resources. } // Dispose unmanaged resources. ClosePort(); WrappedSerialPort.DataReceived -= SerialPortDataReceived; _wrappedSerialPort.Dispose(); _disposed = true; } } ~SerialPortAdapter() { Dispose(false); } #endregion } 

Edit: is it necessary to call or is it enough to call _wrappedSerialPort.Dispose () ;?

  ClosePort(); WrappedSerialPort.DataReceived -= SerialPortDataReceived; _wrappedSerialPort.Dispose(); 
+6
c # serial-port
source share
4 answers

Henk Holterman's answer is correct: SerialPort is a managed resource that itself owns an unmanaged resource and, therefore, implements IDisposable .

Since your shell owns SerialPort, it indirectly owns the unmanaged SerialPort resource and, therefore, must implement IDisposable. Your implementation is incorrect, your own instance of SerialPort should only be removed if disposing is true, since it is a managed resource.

It should be implemented as follows:

  private void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Dispose managed resources. ClosePort(); WrappedSerialPort.DataReceived -= SerialPortDataReceived; _wrappedSerialPort.Dispose(); } _disposed = true; } } 

In addition, as Henk Holterman points out, you only need a destructor if you directly own unmanaged resources, which is not the case here, and you can simplify the implementation of IDisposable by getting rid of the destructor.

+5
source share

SerialPort itself is the owner of an unmanaged resource, so it implements a full one-time template.

In your class, _wrappedSerialPort is a managed resource. My definition: a managed resource is an indirect unmanaged resource.

Your class does not need a complete template. You can and should omit the destructor (or finalizer), ~SerialPortAdapter () and after that you can skip SupressFinalize.

It's best to leave the rest, but you'll see that it will be much easier to shorten the code (because void Dispose(bool) will never be called using false ).

+6
source share
  • Yes, a serial bank is an unmanaged report. Or have you ever seen a garbage collector for PHYSICAL EQUIPMENT?;)

  • This is shown by the class for accessing the IDisposable implementation - this means that your class that hosts the term "long time" refers to a lot of MUST BE USED FOR IDENTIFICATION, TOO.

+1
source share

Yes, you are correctly executing the Dispose method in this case. Remember to add IDisposable to the class declaration. This allows you to use a very convenient using construct, for example:

 using (var port = new SerialPortAdapter(serialPort)) { port.OpenPort(); // use port } 

A call to Dispose on the completed serial port should be sufficient, as this will close the port. According to MS documentation, Close will call Dispose internally. There is no harm in being clear about your intentions by explicitly calling Close , however.

Unregistering an event, like you, is a good practice.

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.close.aspx

0
source share

All Articles