We know Dispose(bool disposing) should be protected or private, what if i need to manually release the unmanage resources? Dispose() from interface IDISPOSIBLE must call Dispose(true) which means release all resource. i want to manually control the release of manage and unmanage resouces.
The official way to implement Dispose is https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose . however sometime i need to manually release the certain resource by use Dispose(false) should this function be public or do i need create another function like DisposeUnManage() for dispose unmanage resource manually?
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing) {
handle.Dispose();
// Free any other managed objects here.
}
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void DisposeUnmanage()
{
Dispose(false);
}
private void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing) {
handle.Dispose();
// Free any other managed objects here.
}
disposed = true;
}
like this code from TCPStream, i need to use this TCPStream.Dispose(false) method when a TCP client is disconnected. when my TCPServer shutdown i should call TCPStream.Dispose(true).
/// <summary>
/// Closes the underlying socket
/// </summary>
/// <param name="disposing">
/// If true, the EventArg objects will be disposed instead of being re-added to
/// the IO pool. This should NEVER be set to true unless we are shutting down the server!
/// </param>
private void Dispose(bool disposeEventArgs = false)
{
// Set that the socket is being closed once, and properly
if (SocketClosed) return;
SocketClosed = true;
// If we need to dispose out EventArgs
if (disposeEventArgs)
{
ReadEventArgs.Dispose();
WriteEventArgs.Dispose();
DisposedEventArgs = true;
}
else
{
// Finally, release this stream so we can allow a new connection
SocketManager.Release(this);
Released = true;
}
// Do a shutdown before you close the socket
try
{
Connection.Shutdown(SocketShutdown.Both);
}
catch (Exception) { }
finally
{
// Unregister for vents
ReadEventArgs.Completed -= IOComplete;
WriteEventArgs.Completed -= IOComplete;
// Close the connection
Connection.Close();
Connection = null;
}
// Call Disconnect Event
if (!DisconnectEventCalled && OnDisconnected != null)
{
DisconnectEventCalled = true;
OnDisconnected();
}
}