private void ImportExcel_Gold(string path) { cancelTokenSrc = new CancellationTokenSource(); var token = cancelTokenSrc.Token; var progressHandler = new Progress<int>(value => { ProgressGold.Value = value; }); var progress = progressHandler as IProgress<int>; GoldDataGrid.Visibility = Visibility.Hidden; GoldProgressGrid.Visibility = Visibility.Visible; ImportGold.IsEnabled = false; ExportGold.IsEnabled = false; try { ProgTextGold.Text = "Checking..."; ExcelExt exl = new ExcelExt(); exl.CancelTokenSource = cancelTokenSrc; Task.Factory.StartNew(() => exl.StartExcelImport(path, token, ProgTextGold)) .ContinueWith((exc) => exl.ExtractGold(exc.Result, progress, token, ProgTextGold)) .ContinueWith((data) => GoldDataGrid.ItemsSource = GoldList = data.Result, TaskScheduler.FromCurrentSynchronizationContext()) .ContinueWith((ui) => ShowUIGold()); } catch (OperationCanceledException) { ProgTextGold.Text = "Cancelled."; } catch (Exception ex) { ProgTextGold.Text = ex.GetType().Name + ": " + ex.Message; } }