/// <summary> /// Start to load data /// </summary> public async Task LoadData() { StringBuilder str = new StringBuilder(); try { var allFavorite = _favoriteStockService.GetAll(); if (allFavorite.Count > 0) { allFavorite = allFavorite.OrderBy(x => x.Order).ToList(); List <RealTimeStockItem> realTimeStockList = await _queryService.GetMultipleRealTimeStockAsync(allFavorite); StockListViewModel.AddStock(realTimeStockList); } } catch (Exception e) { str.AppendLine(e.Message); } if (str.Length != 0) { Dialog.ShowError(str.ToString()); } }
/// <summary> /// Seconds /// </summary> public void StartPollingUpdate(int interval) { UpdateInterval = interval; cancelToken = new CancellationTokenSource(); // 存放目前執行續的CancelTokenSource var localCancelToken = cancelToken; Task.Factory.StartNew(async() => { while (true) { try { await Task.Delay(UpdateInterval * 1000); if (localCancelToken.IsCancellationRequested) { break; } var allFavorite = _favoriteStockService.GetAll(); if (allFavorite.Count > 0) { List <RealTimeStockItem> realTimeStockList = await _stockQueryService.GetMultipleRealTimeStockAsync(allFavorite); foreach (var viewItem in StockList) { var newStockValue = realTimeStockList.FirstOrDefault(x => x.Id == viewItem.Id); if (newStockValue != null) { viewItem.Update(newStockValue); } #if DEBUG else //for test { Dialog.ShowWarning("找不到符合的資料進行更新,股票名稱[" + viewItem.Name + "]"); } #endif } } } catch (Exception e) { Dialog.ShowError(e.Message); } } }, localCancelToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); }