Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/18 15:37:24 (6 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.Common/3.3/Content/ContentManager.cs

    r15583 r16325  
    2121
    2222using System;
     23using System.Threading;
    2324
    2425namespace HeuristicLab.Common {
     
    4344      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    4445      var func = new Func<string, IStorableContent>(instance.LoadContent);
    45       func.BeginInvoke(filename, delegate(IAsyncResult result) {
     46      func.BeginInvoke(filename, delegate (IAsyncResult result) {
    4647        Exception error = null;
    4748        IStorableContent content = null;
     
    4950          content = func.EndInvoke(result);
    5051          content.Filename = filename;
    51         }
    52         catch (Exception ex) {
     52        } catch (Exception ex) {
    5353          error = ex;
    5454        }
     
    5858    protected abstract IStorableContent LoadContent(string filename);
    5959
    60     public static void Save(IStorableContent content, string filename, bool compressed) {
     60    public static void Save(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken = default(CancellationToken)) {
    6161      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    62       instance.SaveContent(content, filename, compressed);
     62      instance.SaveContent(content, filename, compressed, cancellationToken);
    6363      content.Filename = filename;
    6464    }
    65     public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action<IStorableContent, Exception> savingCompletedCallback) {
     65    public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action<IStorableContent, Exception> savingCompletedCallback, CancellationToken cancellationToken = default(CancellationToken)) {
    6666      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    67       var action = new Action<IStorableContent, string, bool>(instance.SaveContent);
    68       action.BeginInvoke(content, filename, compressed, delegate(IAsyncResult result) {
     67      var action = new Action<IStorableContent, string, bool, CancellationToken>(instance.SaveContent);
     68      action.BeginInvoke(content, filename, compressed, cancellationToken, delegate (IAsyncResult result) {
    6969        Exception error = null;
    7070        try {
    7171          action.EndInvoke(result);
    7272          content.Filename = filename;
    73         }
    74         catch (Exception ex) {
     73        } catch (Exception ex) {
    7574          error = ex;
    7675        }
     
    7978
    8079    }
    81     protected abstract void SaveContent(IStorableContent content, string filename, bool compressed);
     80    protected abstract void SaveContent(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken);
    8281  }
    8382}
Note: See TracChangeset for help on using the changeset viewer.