public void Dispose() { if (!_isDisposing) { _isDisposing = true; stopListeningThread(); _messageQueue?.CompleteAdding(); } }
public void Dispose() { if (_disposing) { return; } _disposing = true; if (_client.State == WebSocketState.Open) { try { _client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Client Closing", CancellationToken.None).GetAwaiter().GetResult(); } catch (Exception e) { if (_client.State != WebSocketState.Aborted) { _logError($"Trying to close connection in dispose exception {e} {e.StackTrace}"); } } _closeBehavior(new WebSocketReceivedResultEventArgs(WebSocketCloseStatus.NormalClosure, "Closed because disposing")); } _client.CleanupSendMutex(); _cancellationTokenSource.Cancel(); _cancellationTokenSource.Dispose(); if (_incomingMessagesTask != null) { _incomingMessagesTask.GetAwaiter().GetResult(); _messageQueue.CompleteAdding(); } _client.Dispose(); }