public StockDepthInfoViewModel(StockAgent stockAgent, IDialogService dialogService, StockInfoViewModel stockInfoViewModel) { _stockAgent = stockAgent; _dialogService = dialogService; _stockInfoViewModel = stockInfoViewModel; Messenger.Default.Register <GenericMessage <bool> >(this, UpdateStockDepthInfo, b => { lock (this) { if (b.Content && (_updateBuyStockDepthInfo == null || _updateSellStockDepthInfo == null)) { _cts = new CancellationTokenSource(); _updateBuyStockDepthInfo = UpdateBuy(_cts.Token); _updateSellStockDepthInfo = UpdateSell(_cts.Token); } else if (!b.Content && _cts != null && (_updateBuyStockDepthInfo != null || _updateSellStockDepthInfo != null)) { _cts.Cancel(); _updateBuyStockDepthInfo = null; _updateSellStockDepthInfo = null; BuyStockDepthInfoList.Clear(); SellStockDepthInfoList.Clear(); } } }); }
private async Task UpdateBuy(CancellationToken ct) { var t = RefreshTimeSpan; try { while (!ct.IsCancellationRequested) { var addlist = new List <StockDepthInfo>(); var deletelist = new List <StockDepthInfo>(); var s = _stockAgent.Stock_depth(_stockInfoViewModel.CurrentStockInfo.StockId, 0); lock (this) { s.ForEach(x => { var ss = BuyStockDepthInfoList.FirstOrDefault(y => y.Price == x.Price); if (ss != null) { ss.Update(x); } else { var si = new StockDepthInfo(); si.Create(x); addlist.Add(si); } }); BuyStockDepthInfoList.ForEach(x => { var ss = s.FirstOrDefault(y => y.Price == x.Price); if (ss == null) { deletelist.Add(x); } }); addlist.ForEach(x => BuyStockDepthInfoList.Add(x)); deletelist.ForEach(x => BuyStockDepthInfoList.Remove(x)); BuyStockDepthInfoList = SortStockDepthInfoList(BuyStockDepthInfoList); } await Task.Delay(t, ct); } } catch (Exception e) { if (!ct.IsCancellationRequested) { await _dialogService.ShowError(e, "错误", "确定", null); } } }