Browse Source

Previewer: minimize the risk of a race condition, when Avalonia is still using the images and code disposes them

Marcin Ziąbek 1 year ago
parent
commit
5f4fbb6971
1 changed files with 11 additions and 3 deletions
  1. 11 3
      Source/QuestPDF.Previewer/InteractiveCanvas.cs

+ 11 - 3
Source/QuestPDF.Previewer/InteractiveCanvas.cs

@@ -60,9 +60,17 @@ class InteractiveCanvas : ICustomDrawOperation
 
     public void SetNewDocumentStructure(DocumentStructure document)
     {
-        foreach (var renderedSnapshot in PageSnapshotCache)
-            renderedSnapshot.Image.Dispose();
-        
+        var imagesToDispose = PageSnapshotCache.Select(x => x.Image).ToList();
+
+        // minimize the risk of a race condition, when Avalonia is still using the images and code disposes them
+        Task.Run(async () =>
+        {
+            await Task.Delay(TimeSpan.FromSeconds(5));
+            
+            foreach (var image in imagesToDispose)
+                image.Dispose();
+        });
+
         PageSnapshotCache.Clear();
         PageSizes = document.Pages.ToList();
     }