我应该什么时候调用 SerialPort.Dispose() vs SerialPort.Close()?
When should I call SerialPort.Dispose() vs SerialPort.Close()?
我有一个 SerialPort
用于连接到虚拟 COM 端口。由于连接是持久的,我必须保留对 SerialPort
的引用才能打开、关闭和管理端口。我还在我的 class 上实现 IDisposable
(不是完整的 Dispose 模式,因为我实际上没有任何适当的非托管资源,只有 SerialPort
)。
我的问题与 SerialPort.Dispose()
与 SerialPort.Close()
的使用有关。我在几个地方使用了 Close()
,我从文档中了解到这会调用 SerialPort
上的 Dispose()
方法。但是,如果在我的 TryConnect()
方法中,SerialPort
可能从未打开过怎么办?我应该简单地调用 Dispose()
并保留它吗?或者 Close()
方法是更好的选择吗?
更广泛地说,使用其中一种方法而不是另一种方法总是一个好主意吗?
下面是我的代码中的一些相关片段。
public bool TryConnect() {
CheckDisposed();
try {
connectedPort = new SerialPort(SelectedPort);
connectedPort.WriteTimeout = 1000;
connectedPort.DataReceived += P_DataReceived;
connectedPort.Open();
return true;
} catch (Exception e) {
if (connectedPort != null) {
connectedPort.Dispose();
connectedPort = null;
}
return false;
}
}
public void Disconnect() {
CheckDisposed();
if (connectedPort != null) {
connectedPort.Close();
connectedPort = null;
}
}
public void Dispose() {
if (!disposed) {
if (connectedPort != null) {
connectedPort.Close();
connectedPort = null;
}
disposed = true;
}
}
调用Close
等于调用Dispose(true)
https://github.com/Microsoft/referencesource/blob/master/System/sys/system/IO/ports/SerialPort.cs
// Calls internal Serial Stream's Close() method on the internal Serial Stream.
public void Close()
{
Dispose();
}
protected override void Dispose( bool disposing )
{
if( disposing ) {
if (IsOpen) {
internalSerialStream.Flush();
internalSerialStream.Close();
internalSerialStream = null;
}
}
base.Dispose( disposing );
}
对于此 class,Close
与 Dispose
相同。使用 ILSpy,这是 Close
方法的代码:
public void Close()
{
base.Dispose();
}
我有一个 SerialPort
用于连接到虚拟 COM 端口。由于连接是持久的,我必须保留对 SerialPort
的引用才能打开、关闭和管理端口。我还在我的 class 上实现 IDisposable
(不是完整的 Dispose 模式,因为我实际上没有任何适当的非托管资源,只有 SerialPort
)。
我的问题与 SerialPort.Dispose()
与 SerialPort.Close()
的使用有关。我在几个地方使用了 Close()
,我从文档中了解到这会调用 SerialPort
上的 Dispose()
方法。但是,如果在我的 TryConnect()
方法中,SerialPort
可能从未打开过怎么办?我应该简单地调用 Dispose()
并保留它吗?或者 Close()
方法是更好的选择吗?
更广泛地说,使用其中一种方法而不是另一种方法总是一个好主意吗?
下面是我的代码中的一些相关片段。
public bool TryConnect() {
CheckDisposed();
try {
connectedPort = new SerialPort(SelectedPort);
connectedPort.WriteTimeout = 1000;
connectedPort.DataReceived += P_DataReceived;
connectedPort.Open();
return true;
} catch (Exception e) {
if (connectedPort != null) {
connectedPort.Dispose();
connectedPort = null;
}
return false;
}
}
public void Disconnect() {
CheckDisposed();
if (connectedPort != null) {
connectedPort.Close();
connectedPort = null;
}
}
public void Dispose() {
if (!disposed) {
if (connectedPort != null) {
connectedPort.Close();
connectedPort = null;
}
disposed = true;
}
}
调用Close
等于调用Dispose(true)
https://github.com/Microsoft/referencesource/blob/master/System/sys/system/IO/ports/SerialPort.cs
// Calls internal Serial Stream's Close() method on the internal Serial Stream.
public void Close()
{
Dispose();
}
protected override void Dispose( bool disposing )
{
if( disposing ) {
if (IsOpen) {
internalSerialStream.Flush();
internalSerialStream.Close();
internalSerialStream = null;
}
}
base.Dispose( disposing );
}
Close
与 Dispose
相同。使用 ILSpy,这是 Close
方法的代码:
public void Close()
{
base.Dispose();
}