diff --git a/GeothermalResearchInstitute/GeothermalResearchInstitute.PlcV2/PlcClient.cs b/GeothermalResearchInstitute/GeothermalResearchInstitute.PlcV2/PlcClient.cs index 708e04ca..5feb17d2 100644 --- a/GeothermalResearchInstitute/GeothermalResearchInstitute.PlcV2/PlcClient.cs +++ b/GeothermalResearchInstitute/GeothermalResearchInstitute.PlcV2/PlcClient.cs @@ -31,6 +31,7 @@ public partial class PlcClient : IDisposable private readonly Task sendingBackgroundTask; private readonly Task receivingBackgroundTask; private readonly Task deadlineBackgroundTask; + private readonly SemaphoreSlim mutex = new SemaphoreSlim(1); private readonly CancellationTokenSource closingCancellationTokenSource = new CancellationTokenSource(); private readonly ConcurrentDictionary requestContextReceivingDictionary = @@ -83,17 +84,25 @@ public void Dispose() public async Task Close() { - if (!this.closingCancellationTokenSource.IsCancellationRequested) + await this.mutex.WaitAsync().ConfigureAwait(false); + try { - this.closingCancellationTokenSource.Cancel(); - this.requestContextSendingBufferBlock.Complete(); + if (!this.closingCancellationTokenSource.IsCancellationRequested) + { + this.closingCancellationTokenSource.Cancel(); + this.requestContextSendingBufferBlock.Complete(); - await this.sendingBackgroundTask.ConfigureAwait(false); - await this.receivingBackgroundTask.ConfigureAwait(false); - await this.deadlineBackgroundTask.ConfigureAwait(false); + await this.sendingBackgroundTask.ConfigureAwait(false); + await this.receivingBackgroundTask.ConfigureAwait(false); + await this.deadlineBackgroundTask.ConfigureAwait(false); - this.tcpClient.Close(); - this.OnClosed?.Invoke(this, EventArgs.Empty); + this.tcpClient.Close(); + this.OnClosed?.Invoke(this, EventArgs.Empty); + } + } + finally + { + this.mutex.Release(); } } @@ -111,6 +120,7 @@ protected virtual void Dispose(bool disposing) this.tcpClient.Dispose(); this.closingCancellationTokenSource.Dispose(); + this.mutex.Dispose(); } this.disposedValue = true;