mirror of
				https://github.com/JKorf/CryptoExchange.Net
				synced 2025-11-04 04:17:32 +00:00 
			
		
		
		
	Merge pull request #141 from nathan-datusarator/master
Add checks for Disposed
This commit is contained in:
		
						commit
						8fe00693bd
					
				@ -258,24 +258,32 @@ namespace CryptoExchange.Net.OrderBook
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _subscription = startResult.Data;
 | 
					            _subscription = startResult.Data;
 | 
				
			||||||
            _subscription.ConnectionLost += () =>
 | 
					            _subscription.ConnectionLost += HandleConnectionLost;
 | 
				
			||||||
            {
 | 
					            _subscription.ConnectionClosed += HandleConnectionClosed;
 | 
				
			||||||
                log.Write(LogLevel.Warning, $"{Id} order book {Symbol} connection lost");
 | 
					            _subscription.ConnectionRestored += HandleConnectionRestored;
 | 
				
			||||||
                Status = OrderBookStatus.Reconnecting;
 | 
					 | 
				
			||||||
                Reset();
 | 
					 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
            _subscription.ConnectionClosed += () =>
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                log.Write(LogLevel.Warning, $"{Id} order book {Symbol} disconnected");
 | 
					 | 
				
			||||||
                Status = OrderBookStatus.Disconnected;
 | 
					 | 
				
			||||||
                _ = StopAsync();
 | 
					 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _subscription.ConnectionRestored += async time => await ResyncAsync().ConfigureAwait(false);
 | 
					 | 
				
			||||||
            Status = OrderBookStatus.Synced;
 | 
					            Status = OrderBookStatus.Synced;
 | 
				
			||||||
            return new CallResult<bool>(true);
 | 
					            return new CallResult<bool>(true);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void HandleConnectionLost() {
 | 
				
			||||||
 | 
					             log.Write(LogLevel.Warning, $"{Id} order book {Symbol} connection lost");
 | 
				
			||||||
 | 
					             if (Status != OrderBookStatus.Disposed) {
 | 
				
			||||||
 | 
					                Status = OrderBookStatus.Reconnecting;
 | 
				
			||||||
 | 
					                Reset();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void HandleConnectionClosed() {
 | 
				
			||||||
 | 
					            log.Write(LogLevel.Warning, $"{Id} order book {Symbol} disconnected");
 | 
				
			||||||
 | 
					            Status = OrderBookStatus.Disconnected;
 | 
				
			||||||
 | 
					            _ = StopAsync();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private async void HandleConnectionRestored(TimeSpan _) {
 | 
				
			||||||
 | 
					            await ResyncAsync().ConfigureAwait(false);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <inheritdoc/>
 | 
					        /// <inheritdoc/>
 | 
				
			||||||
        public async Task StopAsync()
 | 
					        public async Task StopAsync()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@ -286,8 +294,12 @@ namespace CryptoExchange.Net.OrderBook
 | 
				
			|||||||
            if (_processTask != null)
 | 
					            if (_processTask != null)
 | 
				
			||||||
                await _processTask.ConfigureAwait(false);
 | 
					                await _processTask.ConfigureAwait(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (_subscription != null)
 | 
					            if (_subscription != null) {
 | 
				
			||||||
                await _subscription.CloseAsync().ConfigureAwait(false);
 | 
					                await _subscription.CloseAsync().ConfigureAwait(false);
 | 
				
			||||||
 | 
					                _subscription.ConnectionLost -= HandleConnectionLost;
 | 
				
			||||||
 | 
					                _subscription.ConnectionClosed -= HandleConnectionClosed;
 | 
				
			||||||
 | 
					                _subscription.ConnectionRestored -= HandleConnectionRestored;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            log.Write(LogLevel.Trace, $"{Id} order book {Symbol} stopped");
 | 
					            log.Write(LogLevel.Trace, $"{Id} order book {Symbol} stopped");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -601,13 +613,13 @@ namespace CryptoExchange.Net.OrderBook
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        private async Task ProcessQueue()
 | 
					        private async Task ProcessQueue()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            while (Status != OrderBookStatus.Disconnected)
 | 
					            while (Status != OrderBookStatus.Disconnected && Status != OrderBookStatus.Disposed)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                await _queueEvent.WaitAsync().ConfigureAwait(false);
 | 
					                await _queueEvent.WaitAsync().ConfigureAwait(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                while (_processQueue.TryDequeue(out var item))
 | 
					                while (_processQueue.TryDequeue(out var item))
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    if (Status == OrderBookStatus.Disconnected)
 | 
					                    if (Status == OrderBookStatus.Disconnected || Status == OrderBookStatus.Disposed)
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (_stopProcessing)
 | 
					                    if (_stopProcessing)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user