private void ExportExcel_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; var _task = Task.Factory.StartNew(() => exl.StartExcelExport(path, token, ProgTextStone)) .ContinueWith((exc) => exl.CreateTemplate(progress, token, ProgTextStone, StoneList)) .ContinueWith((save) => exl.SaveExport(save.Result, "Stone")) .ContinueWith((ui) => ShowUIStone()); } catch (OperationCanceledException) { ProgTextStone.Text = "Cancelled."; } catch (Exception ex) { ProgTextStone.Text = ex.GetType().Name + ": " + ex.Message; } }