Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/18 15:37:24 (5 years ago)
Author:
pfleck
Message:

#2965

  • Added CancelationTokens for the Save and Serialize methods.
  • Fixed a potential temp-file-leak when replacing the old file with the new one after serialization.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2965_CancelablePersistence/HeuristicLab.Optimizer/3.3/FileManager.cs

    r15583 r16325  
    2323using System.Collections.Generic;
    2424using System.IO;
     25using System.Threading;
    2526using System.Windows.Forms;
    2627using HeuristicLab.Common;
     
    7879        if (view == null)
    7980          ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
    80       }
    81       catch (Exception ex) {
     81      } catch (Exception ex) {
    8282        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
    83       }
    84       finally {
     83      } finally {
    8584        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
    8685      }
     
    10099        else {
    101100          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
     101          var cancellationTokenSource = new CancellationTokenSource();
    102102          SetSaveOperationProgressInContentViews(content, true);
    103           ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
     103          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted, cancellationTokenSource.Token);
    104104        }
    105105      }
     
    132132        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
    133133          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
    134           SetSaveOperationProgressInContentViews(content, true, saveFileDialog.FileName);
    135           if (saveFileDialog.FilterIndex == 1) {
    136             ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
    137           } else {
    138             ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
    139           }
     134          bool compressed = saveFileDialog.FilterIndex != 1;
     135          var cancellationTokenSource = new CancellationTokenSource();
     136          SetSaveOperationProgressInContentViews(content, compressed, saveFileDialog.FileName);
     137
     138          ContentManager.SaveAsync(content, saveFileDialog.FileName, compressed, SavingCompleted, cancellationTokenSource.Token);
    140139        }
    141140      }
     
    145144        if (error != null) throw error;
    146145        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
    147       }
    148       catch (Exception ex) {
     146      } catch (Exception ex) {
    149147        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
    150       }
    151       finally {
     148      } finally {
    152149        SetSaveOperationProgressInContentViews(content, false);
    153150        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
     
    167164      #endregion
    168165    }
     166
     167    /* For later merge with #2845
     168    private static void AddProgressInContentViews(IStorableContent content, CancellationTokenSource cancellationTokenSource, string fileName = null) {
     169      string message = string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename));
     170      Progress.Show(content, message, ProgressMode.Indeterminate, cancelRequestHandler: () => cancellationTokenSource.Cancel());
     171    }*/
    169172  }
    170173}
Note: See TracChangeset for help on using the changeset viewer.