public void Listen(CancellationToken token) { try { _api.ConnectionEvents.ConnectedEventHandler += ConnectionEvents_ConnectedEventHandler; _api.ConnectionEvents.DisconnectedEventHandler += ConnectionEvents_DisconnectedEventHandler; _api.DealEvents.DealAddEventHandler += DealEvents_DealAddEventHandler; _api.Connect(_connection); while (!token.IsCancellationRequested) { if (_rawTradeEvents.TryDequeue(out RawTradeEvent rawTrade)) { var balance = _api.GetUserBalance(rawTrade.Login); var trade = new Trade(_connection.IP, rawTrade, balance); _monitor.QueuedTrades.Enqueue(trade); } } token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { Console.WriteLine($"Stopping {_connection.Name}"); } catch (Exception e) { Console.WriteLine($"Error while listening on {_connection.Name}"); Console.WriteLine(e); Console.WriteLine(); throw; } finally { _api.Disconnect(); _api.ConnectionEvents.ConnectedEventHandler -= ConnectionEvents_ConnectedEventHandler; _api.ConnectionEvents.DisconnectedEventHandler -= ConnectionEvents_DisconnectedEventHandler; _api.DealEvents.DealAddEventHandler -= DealEvents_DealAddEventHandler; } }