Ignore:
Timestamp:
12/21/18 14:43:42 (2 years ago)
Author:
pfleck
Message:

#2965 merged branch to trunk

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/HeuristicLab.Optimizer

  • trunk/HeuristicLab.Optimizer/3.3/FileManager.cs

    r16430 r16440  
    2323using System.Collections.Generic;
    2424using System.IO;
     25using System.Threading;
    2526using System.Windows.Forms;
    2627using HeuristicLab.Common;
     
    9899        else {
    99100          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
    100           SetSaveOperationProgressInContentViews(content, true);
    101           ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
     101          var cancellationTokenSource = new CancellationTokenSource();
     102          AddProgressInContentViews(content, cancellationTokenSource);
     103          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted, cancellationTokenSource.Token);
    102104        }
    103105      }
     
    130132        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
    131133          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
    132           SetSaveOperationProgressInContentViews(content, true, saveFileDialog.FileName);
    133           if (saveFileDialog.FilterIndex == 1) {
    134             ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
    135           } else {
    136             ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
    137           }
     134          bool compressed = saveFileDialog.FilterIndex != 1;
     135          var cancellationTokenSource = new CancellationTokenSource();
     136          AddProgressInContentViews(content, cancellationTokenSource, saveFileDialog.FileName);
     137
     138          ContentManager.SaveAsync(content, saveFileDialog.FileName, compressed, SavingCompleted, cancellationTokenSource.Token);
    138139        }
    139140      }
     
    143144        if (error != null) throw error;
    144145        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
     146      } catch (OperationCanceledException) { // do nothing if canceled
    145147      } catch (Exception ex) {
    146148        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
    147149      } finally {
    148         SetSaveOperationProgressInContentViews(content, false);
     150        Progress.Hide(content);
    149151        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
    150152      }
    151153    }
    152154
    153     private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null) {
    154       HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
    155       #region Mono Compatibility
    156       // removed the InvokeRequired check because of Mono
    157       mainForm.Invoke((Action)delegate {
    158         if (showProgress) {
    159           Progress.Show(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
    160         } else
    161           Progress.Hide(content);
    162       });
    163       #endregion
     155    private static void AddProgressInContentViews(IStorableContent content, CancellationTokenSource cancellationTokenSource, string fileName = null) {
     156      string message = string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename));
     157      Progress.Show(content, message, ProgressMode.Indeterminate, cancelRequestHandler: () => cancellationTokenSource.Cancel());
    164158    }
    165159  }
Note: See TracChangeset for help on using the changeset viewer.