diff --git a/Source/HiveMQtt/Client/Connection/ConnectionManager.cs b/Source/HiveMQtt/Client/Connection/ConnectionManager.cs index 5343f4d1..47da4a8f 100644 --- a/Source/HiveMQtt/Client/Connection/ConnectionManager.cs +++ b/Source/HiveMQtt/Client/Connection/ConnectionManager.cs @@ -197,6 +197,10 @@ internal async Task CancelBackgroundTasksAsync() /// public void Dispose() { + // Dispose managed resources. + this.cancellationTokenSource.Cancel(); + this.cancellationTokenSource.Dispose(); + this.Dispose(); /* This object will be cleaned up by the Dispose method. diff --git a/Source/HiveMQtt/Client/HiveMQClientUtil.cs b/Source/HiveMQtt/Client/HiveMQClientUtil.cs index 6289ea0a..63bd166b 100644 --- a/Source/HiveMQtt/Client/HiveMQClientUtil.cs +++ b/Source/HiveMQtt/Client/HiveMQClientUtil.cs @@ -173,11 +173,7 @@ protected virtual void Dispose(bool disposing) Logger.Trace("HiveMQClient Dispose: Disconnecting connected client."); _ = Task.Run(async () => await this.DisconnectAsync().ConfigureAwait(false)); } - - // Dispose managed resources. - // this.cancellationTokenSource.Cancel(); - // this.cancellationTokenSource.Dispose(); - } + } // Call the appropriate methods to clean up // unmanaged resources here.