private void ImportExcel_Stone(string path) { cancelTokenSrc = new CancellationTokenSource(); var token = cancelTokenSrc.Token; var progressHandler = new Progress<int>(value => { ProgressStone.Value = value; }); var progress = progressHandler as IProgress<int>; StoneDataGrid.Visibility = Visibility.Hidden; StoneProgressGrid.Visibility = Visibility.Visible; ImportStone.IsEnabled = false; ExportStone.IsEnabled = false; try { ProgTextStone.Text = "Checking..."; ExcelExt exl = new ExcelExt(); exl.CancelTokenSource = cancelTokenSrc; Task.Factory.StartNew(() => exl.StartExcelImport(path, token, ProgTextStone)) .ContinueWith((exc) => exl.ExtractStone(exc.Result, progress, token, ProgTextStone)) .ContinueWith((data) => StoneDataGrid.ItemsSource = StoneList = data.Result, TaskScheduler.FromCurrentSynchronizationContext()) .ContinueWith((ui) => ShowUIStone()); } catch (OperationCanceledException) { ProgTextStone.Text = "Cancelled."; } catch (Exception ex) { ProgTextStone.Text = ex.GetType().Name + ": " + ex.Message; } }