Changeset 16325
- Timestamp:
- 11/23/18 15:37:24 (6 years ago)
- Location:
- branches/2965_CancelablePersistence
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2965_CancelablePersistence/HeuristicLab.Common/3.3/Content/ContentManager.cs
r15583 r16325 21 21 22 22 using System; 23 using System.Threading; 23 24 24 25 namespace HeuristicLab.Common { … … 43 44 if (instance == null) throw new InvalidOperationException("ContentManager is not initialized."); 44 45 var func = new Func<string, IStorableContent>(instance.LoadContent); 45 func.BeginInvoke(filename, delegate (IAsyncResult result) {46 func.BeginInvoke(filename, delegate (IAsyncResult result) { 46 47 Exception error = null; 47 48 IStorableContent content = null; … … 49 50 content = func.EndInvoke(result); 50 51 content.Filename = filename; 51 } 52 catch (Exception ex) { 52 } catch (Exception ex) { 53 53 error = ex; 54 54 } … … 58 58 protected abstract IStorableContent LoadContent(string filename); 59 59 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)) { 61 61 if (instance == null) throw new InvalidOperationException("ContentManager is not initialized."); 62 instance.SaveContent(content, filename, compressed );62 instance.SaveContent(content, filename, compressed, cancellationToken); 63 63 content.Filename = filename; 64 64 } 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)) { 66 66 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) { 69 69 Exception error = null; 70 70 try { 71 71 action.EndInvoke(result); 72 72 content.Filename = filename; 73 } 74 catch (Exception ex) { 73 } catch (Exception ex) { 75 74 error = ex; 76 75 } … … 79 78 80 79 } 81 protected abstract void SaveContent(IStorableContent content, string filename, bool compressed );80 protected abstract void SaveContent(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken); 82 81 } 83 82 } -
branches/2965_CancelablePersistence/HeuristicLab.Core/3.3/PersistenceContentManager.cs
r15583 r16325 21 21 22 22 using System.IO.Compression; 23 using System.Threading; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Persistence.Default.Xml; … … 32 33 } 33 34 34 protected override void SaveContent(IStorableContent content, string filename, bool compressed ) {35 XmlGenerator.Serialize(content, filename, compressed ? CompressionLevel.Optimal : CompressionLevel.NoCompression );35 protected override void SaveContent(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken) { 36 XmlGenerator.Serialize(content, filename, compressed ? CompressionLevel.Optimal : CompressionLevel.NoCompression, cancellationToken); 36 37 } 37 38 } -
branches/2965_CancelablePersistence/HeuristicLab.Optimizer/3.3/FileManager.cs
r15583 r16325 23 23 using System.Collections.Generic; 24 24 using System.IO; 25 using System.Threading; 25 26 using System.Windows.Forms; 26 27 using HeuristicLab.Common; … … 78 79 if (view == null) 79 80 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) { 82 82 ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex); 83 } 84 finally { 83 } finally { 85 84 ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor(); 86 85 } … … 100 99 else { 101 100 MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor(); 101 var cancellationTokenSource = new CancellationTokenSource(); 102 102 SetSaveOperationProgressInContentViews(content, true); 103 ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted );103 ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted, cancellationTokenSource.Token); 104 104 } 105 105 } … … 132 132 if (saveFileDialog.ShowDialog() == DialogResult.OK) { 133 133 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); 140 139 } 141 140 } … … 145 144 if (error != null) throw error; 146 145 MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle(); 147 } 148 catch (Exception ex) { 146 } catch (Exception ex) { 149 147 ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex); 150 } 151 finally { 148 } finally { 152 149 SetSaveOperationProgressInContentViews(content, false); 153 150 MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor(); … … 167 164 #endregion 168 165 } 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 }*/ 169 172 } 170 173 } -
branches/2965_CancelablePersistence/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r15583 r16325 26 26 using System.Linq; 27 27 using System.Text; 28 using System.Threading; 28 29 using HeuristicLab.Persistence.Core; 29 30 using HeuristicLab.Persistence.Core.Tokens; … … 61 62 } 62 63 63 protected enum NodeType { Start, End, Inline } 64 protected enum NodeType { Start, End, Inline }; 64 65 65 66 protected static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, string> attributes) { … … 256 257 {"typeName", lastTypeToken.TypeName }, 257 258 {"serializer", lastTypeToken.Serializer }}); 258 } 259 finally { 259 } finally { 260 260 lastTypeToken = null; 261 261 } … … 284 284 /// <param name="o">The object.</param> 285 285 /// <param name="filename">The filename.</param> 286 public static void Serialize(object o, string filename ) {287 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, CompressionLevel.Optimal );286 public static void Serialize(object o, string filename, CancellationToken cancellationToken = default(CancellationToken)) { 287 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, CompressionLevel.Optimal, cancellationToken); 288 288 } 289 289 … … 296 296 /// <param name="filename">The filename.</param> 297 297 /// <param name="compression">ZIP file compression level</param> 298 public static void Serialize(object o, string filename, CompressionLevel compression ) {299 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression );298 public static void Serialize(object o, string filename, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) { 299 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression, cancellationToken); 300 300 } 301 301 … … 307 307 /// <param name="filename">The filename.</param> 308 308 /// <param name="config">The configuration.</param> 309 public static void Serialize(object obj, string filename, Configuration config) { 310 Serialize(obj, filename, config, false, CompressionLevel.Optimal); 311 } 312 313 private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies 314 , CompressionLevel compression) { 309 public static void Serialize(object obj, string filename, Configuration config, CancellationToken cancellationToken = default(CancellationToken)) { 310 Serialize(obj, filename, config, false, CompressionLevel.Optimal, cancellationToken); 311 } 312 313 private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) { 315 314 Serializer serializer = new Serializer(obj, config); 316 Serialize(stream, includeAssemblies, compression, serializer );317 } 318 319 private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer ) {315 Serialize(stream, includeAssemblies, compression, serializer, cancellationToken); 316 } 317 318 private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer, CancellationToken cancellationToken = default(CancellationToken)) { 320 319 try { 320 cancellationToken.ThrowIfCancellationRequested(); 321 321 DateTime start = DateTime.Now; 322 322 serializer.InterleaveTypeInformation = false; … … 326 326 using (StreamWriter writer = new StreamWriter(entry.Open())) { 327 327 foreach (ISerializationToken token in serializer) { 328 cancellationToken.ThrowIfCancellationRequested(); 328 329 string line = generator.Format(token); 329 330 writer.Write(line); … … 338 339 if (includeAssemblies) { 339 340 foreach (string name in serializer.RequiredFiles) { 341 cancellationToken.ThrowIfCancellationRequested(); 340 342 Uri uri = new Uri(name); 341 343 if (!uri.IsFile) { … … 348 350 byte[] buffer = new byte[1024 * 1024]; 349 351 while (true) { 352 cancellationToken.ThrowIfCancellationRequested(); 350 353 int bytesRead = reader.Read(buffer, 0, 1024 * 1024); 351 354 if (bytesRead == 0) … … 360 363 Logger.Info(String.Format("serialization took {0} seconds with compression level {1}", 361 364 (DateTime.Now - start).TotalSeconds, compression)); 362 } 363 catch (Exception) { 365 } catch (Exception) { 364 366 Logger.Warn("Exception caught, no data has been serialized."); 365 367 throw; … … 375 377 /// <param name="includeAssemblies">if set to <c>true</c> include needed assemblies.</param> 376 378 /// <param name="compression">The ZIP compression level.</param> 377 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression) { 379 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression, CancellationToken cancellationToken = default(CancellationToken)) { 380 string tempfile = null; 378 381 try { 379 stringtempfile = Path.GetTempFileName();382 tempfile = Path.GetTempFileName(); 380 383 381 384 using (FileStream stream = File.Create(tempfile)) { 382 Serialize(obj, stream, config, includeAssemblies, compression );383 } 384 385 Serialize(obj, stream, config, includeAssemblies, compression, cancellationToken); 386 } 387 // copy only if needed 385 388 File.Copy(tempfile, filename, true); 386 File.Delete(tempfile); 387 } 388 catch (Exception) { 389 } catch (Exception) { 389 390 Logger.Warn("Exception caught, no data has been written."); 390 391 throw; 392 } finally { 393 if (tempfile != null && File.Exists(tempfile)) 394 File.Delete(tempfile); 391 395 } 392 396 } … … 399 403 /// <param name="stream">The stream.</param> 400 404 /// <param name="compressionType">Type of compression, default is GZip.</param> 401 public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip ) {402 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType );405 public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) { 406 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType, cancellationToken); 403 407 } 404 408 … … 411 415 /// <param name="config">The configuration.</param> 412 416 /// <param name="compressionType">Type of compression, default is GZip.</param> 413 public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip ) {414 Serialize(obj, stream, config, false, compressionType );417 public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) { 418 Serialize(obj, stream, config, false, compressionType, cancellationToken); 415 419 } 416 420 … … 423 427 /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param> 424 428 /// <param name="compressionType">Type of compression, default is GZip.</param> 425 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, 426 CompressionType compressionType = CompressionType.GZip) { 429 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) { 427 430 try { 428 431 Serializer serializer = new Serializer(obj, config); 429 432 if (compressionType == CompressionType.Zip) { 430 Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal );433 Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal, cancellationToken); 431 434 } else { 432 Serialize(stream, serializer); 433 } 434 } 435 catch (PersistenceException) { 435 Serialize(stream, serializer, cancellationToken); 436 } 437 } catch (PersistenceException) { 436 438 throw; 437 } 438 catch (Exception e) { 439 } catch (Exception e) { 439 440 throw new PersistenceException("Unexpected exception during Serialization.", e); 440 441 } … … 451 452 /// <param name="compressionType">Type of compression, default is GZip.</param> 452 453 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types, 453 CompressionType compressionType = CompressionType.GZip ) {454 CompressionType compressionType = CompressionType.GZip, CancellationToken cancellationToken = default(CancellationToken)) { 454 455 try { 455 456 Serializer serializer = new Serializer(obj, config); 456 457 if (compressionType == CompressionType.Zip) { 457 Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer );458 Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer, cancellationToken); 458 459 } else { 459 Serialize(stream, serializer );460 Serialize(stream, serializer, cancellationToken); 460 461 } 461 462 types = serializer.SerializedTypes; 462 } 463 catch (PersistenceException) { 463 } catch (PersistenceException) { 464 464 throw; 465 } 466 catch (Exception e) { 465 } catch (Exception e) { 467 466 throw new PersistenceException("Unexpected exception during Serialization.", e); 468 467 } 469 468 } 470 469 471 private static void Serialize(Stream stream, Serializer serializer) { 470 private static void Serialize(Stream stream, Serializer serializer, CancellationToken cancellationToken = default(CancellationToken)) { 471 cancellationToken.ThrowIfCancellationRequested(); 472 472 using (StreamWriter writer = new StreamWriter(new GZipStream(stream, CompressionMode.Compress))) { 473 473 serializer.InterleaveTypeInformation = true; 474 474 XmlGenerator generator = new XmlGenerator(); 475 475 foreach (ISerializationToken token in serializer) { 476 cancellationToken.ThrowIfCancellationRequested(); 476 477 string line = generator.Format(token); 477 478 writer.Write(line);
Note: See TracChangeset
for help on using the changeset viewer.