private async Task Update(CancellationToken ct) { var t = RefreshTimeSpan; try { while (!ct.IsCancellationRequested) { var addlist = new List <TransactionRecentInfo>(); var deletelist = new List <TransactionRecentInfo>(); var s = _stockAgent.GetRecentTrans(_stockInfoViewModel.CurrentStockInfo.StockId, RecentNum); lock (this) { s.ForEach(x => { var ss = TransactionRecentInfoList.FirstOrDefault(y => y.TranId == x.TranId); if (ss != null) { return; } var si = new TransactionRecentInfo(); si.Create(x); addlist.Add(si); }); TransactionRecentInfoList.ForEach(x => { var ss = s.FirstOrDefault(y => y.TranId == x.TranId); if (ss == null) { deletelist.Add(x); } }); addlist.ForEach(x => TransactionRecentInfoList.Add(x)); deletelist.ForEach(x => TransactionRecentInfoList.Remove(x)); TransactionRecentInfoList = TransactionRecentInfoList.OrderByDescending(x => x.CreateDateTime).ToList(); } await Task.Delay(t, ct); } } catch (Exception e) { if (!ct.IsCancellationRequested) { await _dialogService.ShowError(e, "错误", "确定", null); } } }
public TransactionRecentInfoViewModel(StockAgent stockAgent, IDialogService dialogService, StockInfoViewModel stockInfoViewModel) { _stockAgent = stockAgent; _dialogService = dialogService; _stockInfoViewModel = stockInfoViewModel; Messenger.Default.Register <GenericMessage <bool> >(this, UpdateTransactionRecentInfo, b => { lock (this) { if (b.Content && _updateTransactionRecentInfo == null) { _cts = new CancellationTokenSource(); _updateTransactionRecentInfo = Update(_cts.Token); } else if (!b.Content && _cts != null && _updateTransactionRecentInfo != null) { _cts.Cancel(); _updateTransactionRecentInfo = null; TransactionRecentInfoList.Clear(); } } }); }