private void ImportExcel_Item(string path) { cancelTokenSrc = new CancellationTokenSource(); var token = cancelTokenSrc.Token; var progressHandler = new Progress<int>(value => { ProgressItem.Value = value; }); var progress = progressHandler as IProgress<int>; ItemListGrid.Visibility = Visibility.Hidden; ItemProgressGrid.Visibility = Visibility.Visible; ImportItems.IsEnabled = false; ExportItems.IsEnabled = false; try { ProgTextItem.Text = "Checking..."; ExcelExt exl = new ExcelExt(); exl.CancelTokenSource = cancelTokenSrc; var _task = Task.Factory.StartNew(() => exl.StartExcelImport(path, token, ProgTextItem)) .ContinueWith((exc) => exl.ExtractItems(exc.Result, progress, token, ProgTextItem)) .ContinueWith((data) => ItemCategoryDataGrid.ItemsSource = ItemCategories = data.Result, TaskScheduler.FromCurrentSynchronizationContext()) .ContinueWith((ui) => ShowUIItem()); } catch (OperationCanceledException) { ProgTextItem.Text = "Cancelled."; } catch (Exception ex) { ProgTextItem.Text = ex.GetType().Name + ": " + ex.Message; } }