Index: trunk/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
===================================================================
--- trunk/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs	(revision 16430)
+++ trunk/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs	(revision 16440)
@@ -26,4 +26,5 @@
 using System.Linq;
 using System.Text;
+using System.Threading;
 using HeuristicLab.Persistence.Core;
 using HeuristicLab.Persistence.Core.Tokens;
@@ -61,5 +62,5 @@
     }
 
-    protected enum NodeType { Start, End, Inline } ;
+    protected enum NodeType { Start, End, Inline };
 
     protected static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, string> attributes) {
@@ -256,6 +257,5 @@
           {"typeName", lastTypeToken.TypeName },
           {"serializer", lastTypeToken.Serializer }});
-      }
-      finally {
+      } finally {
         lastTypeToken = null;
       }
@@ -284,6 +284,6 @@
     /// <param name="o">The object.</param>
     /// <param name="filename">The filename.</param>
-    public static void Serialize(object o, string filename) {
-      Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, CompressionLevel.Optimal);
+    public static void Serialize(object o, string filename, CancellationToken cancellationToken = default(CancellationToken)) {
+      Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, CompressionLevel.Optimal, cancellationToken);
     }
 
@@ -296,6 +296,6 @@
     /// <param name="filename">The filename.</param>
     /// <param name="compression">ZIP file compression level</param>
-    public static void Serialize(object o, string filename, CompressionLevel compression) {
-      Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression);
+    public static void Serialize(object o, string filename, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) {
+      Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression, cancellationToken);
     }
 
@@ -307,16 +307,16 @@
     /// <param name="filename">The filename.</param>
     /// <param name="config">The configuration.</param>
-    public static void Serialize(object obj, string filename, Configuration config) {
-      Serialize(obj, filename, config, false, CompressionLevel.Optimal);
-    }
-
-    private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies
-   , CompressionLevel compression) {
+    public static void Serialize(object obj, string filename, Configuration config, CancellationToken cancellationToken = default(CancellationToken)) {
+      Serialize(obj, filename, config, false, CompressionLevel.Optimal, cancellationToken);
+    }
+
+    private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) {
       Serializer serializer = new Serializer(obj, config);
-      Serialize(stream, includeAssemblies, compression, serializer);
-    }
-
-    private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer) {
+      Serialize(stream, includeAssemblies, compression, serializer, cancellationToken);
+    }
+
+    private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer, CancellationToken cancellationToken = default(CancellationToken)) {
       try {
+        cancellationToken.ThrowIfCancellationRequested();
         DateTime start = DateTime.Now;
         serializer.InterleaveTypeInformation = false;
@@ -326,4 +326,5 @@
           using (StreamWriter writer = new StreamWriter(entry.Open())) {
             foreach (ISerializationToken token in serializer) {
+              cancellationToken.ThrowIfCancellationRequested();
               string line = generator.Format(token);
               writer.Write(line);
@@ -338,4 +339,5 @@
           if (includeAssemblies) {
             foreach (string name in serializer.RequiredFiles) {
+              cancellationToken.ThrowIfCancellationRequested();
               Uri uri = new Uri(name);
               if (!uri.IsFile) {
@@ -348,4 +350,5 @@
                   byte[] buffer = new byte[1024 * 1024];
                   while (true) {
+                    cancellationToken.ThrowIfCancellationRequested();
                     int bytesRead = reader.Read(buffer, 0, 1024 * 1024);
                     if (bytesRead == 0)
@@ -360,6 +363,5 @@
         Logger.Info(String.Format("serialization took {0} seconds with compression level {1}",
           (DateTime.Now - start).TotalSeconds, compression));
-      }
-      catch (Exception) {
+      } catch (Exception) {
         Logger.Warn("Exception caught, no data has been serialized.");
         throw;
@@ -375,18 +377,20 @@
     /// <param name="includeAssemblies">if set to <c>true</c> include needed assemblies.</param>
     /// <param name="compression">The ZIP compression level.</param>
-    public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression) {
+    public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) {
+      string tempfile = null;
       try {
-        string tempfile = Path.GetTempFileName();
+        tempfile = Path.GetTempFileName();
 
         using (FileStream stream = File.Create(tempfile)) {
-          Serialize(obj, stream, config, includeAssemblies, compression);
-        }
-
+          Serialize(obj, stream, config, includeAssemblies, compression, cancellationToken);
+        }
+        // copy only if needed
         File.Copy(tempfile, filename, true);
-        File.Delete(tempfile);
-      }
-      catch (Exception) {
+      } catch (Exception) {
         Logger.Warn("Exception caught, no data has been written.");
         throw;
+      } finally {
+        if (tempfile != null && File.Exists(tempfile))
+          File.Delete(tempfile);
       }
     }
@@ -399,6 +403,6 @@
     /// <param name="stream">The stream.</param>
     /// <param name="compressionType">Type of compression, default is GZip.</param>
-    public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip) {
-      Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType);
+    public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) {
+      Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType, cancellationToken);
     }
 
@@ -411,6 +415,6 @@
     /// <param name="config">The configuration.</param>
     /// <param name="compressionType">Type of compression, default is GZip.</param>
-    public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip) {
-      Serialize(obj, stream, config, false, compressionType);
+    public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) {
+      Serialize(obj, stream, config, false, compressionType, cancellationToken);
     }
 
@@ -423,18 +427,15 @@
     /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param>
     /// <param name="compressionType">Type of compression, default is GZip.</param>
-    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies,
-                                 CompressionType compressionType = CompressionType.GZip) {
+    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) {
       try {
         Serializer serializer = new Serializer(obj, config);
         if (compressionType == CompressionType.Zip) {
-          Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal);
+          Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal, cancellationToken);
         } else {
-          Serialize(stream, serializer);
-        }
-      }
-      catch (PersistenceException) {
+          Serialize(stream, serializer, cancellationToken);
+        }
+      } catch (PersistenceException) {
         throw;
-      }
-      catch (Exception e) {
+      } catch (Exception e) {
         throw new PersistenceException("Unexpected exception during Serialization.", e);
       }
@@ -451,27 +452,27 @@
     /// <param name="compressionType">Type of compression, default is GZip.</param>
     public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types,
-                                 CompressionType compressionType = CompressionType.GZip) {
+                                 CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) {
       try {
         Serializer serializer = new Serializer(obj, config);
         if (compressionType == CompressionType.Zip) {
-          Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer);
+          Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer, cancellationToken);
         } else {
-          Serialize(stream, serializer);
+          Serialize(stream, serializer, cancellationToken);
         }
         types = serializer.SerializedTypes;
-      }
-      catch (PersistenceException) {
+      } catch (PersistenceException) {
         throw;
-      }
-      catch (Exception e) {
+      } catch (Exception e) {
         throw new PersistenceException("Unexpected exception during Serialization.", e);
       }
     }
 
-    private static void Serialize(Stream stream, Serializer serializer) {
+    private static void Serialize(Stream stream, Serializer serializer, CancellationToken cancellationToken = default(CancellationToken)) {
+      cancellationToken.ThrowIfCancellationRequested();
       using (StreamWriter writer = new StreamWriter(new GZipStream(stream, CompressionMode.Compress))) {
         serializer.InterleaveTypeInformation = true;
         XmlGenerator generator = new XmlGenerator();
         foreach (ISerializationToken token in serializer) {
+          cancellationToken.ThrowIfCancellationRequested();
           string line = generator.Format(token);
           writer.Write(line);
