瀏覽代碼

Add CancellationToken parameter to ShowInPreviewerAsync (#715)

* Add CancellationToken parameter

* Update PreviewerExtensions.cs

* Update PreviewerExtensions.cs

---------

Co-authored-by: Marcin Ziąbek <[email protected]>
Marc Mognol 2 年之前
父節點
當前提交
cd1d3f27b5
共有 1 個文件被更改,包括 6 次插入8 次删除
  1. 6 8
      Source/QuestPDF/Previewer/PreviewerExtensions.cs

+ 6 - 8
Source/QuestPDF/Previewer/PreviewerExtensions.cs

@@ -1,5 +1,3 @@
-
-
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Threading;
 using System.Threading;
@@ -20,19 +18,19 @@ namespace QuestPDF.Previewer
         }
         }
         
         
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="previewer.supported"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="previewer.supported"]/*' />
-        public static async Task ShowInPreviewerAsync(this IDocument document, int port = 12500)
+        public static async Task ShowInPreviewerAsync(this IDocument document, int port = 12500, CancellationToken cancellationToken = default)
         {
         {
             var previewerService = new PreviewerService(port);
             var previewerService = new PreviewerService(port);
             
             
-            using var cancellationTokenSource = new CancellationTokenSource();
+            using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
             previewerService.OnPreviewerStopped += () => cancellationTokenSource.Cancel();
             previewerService.OnPreviewerStopped += () => cancellationTokenSource.Cancel();
-            
+
             await previewerService.Connect();
             await previewerService.Connect();
             await RefreshPreview();
             await RefreshPreview();
             
             
             HotReloadManager.UpdateApplicationRequested += (_, _) => RefreshPreview();
             HotReloadManager.UpdateApplicationRequested += (_, _) => RefreshPreview();
             
             
-            await WaitForPreviewerExit(cancellationTokenSource.Token);
+            await KeepApplicationAlive(cancellationTokenSource.Token);
             
             
             Task RefreshPreview()
             Task RefreshPreview()
             {
             {
@@ -53,7 +51,7 @@ namespace QuestPDF.Previewer
                 }
                 }
             }
             }
 
 
-            async Task WaitForPreviewerExit(CancellationToken cancellationToken)
+            async Task KeepApplicationAlive(CancellationToken cancellationToken)
             {
             {
                 while (true)
                 while (true)
                 {
                 {
@@ -74,7 +72,7 @@ namespace QuestPDF.Previewer
         }
         }
 
 
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="previewer.notSupported"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="previewer.notSupported"]/*' />
-        public static async Task ShowInPreviewerAsync(this IDocument document, int port = 12500)
+        public static async Task ShowInPreviewerAsync(this IDocument document, int port = 12500, CancellationToken cancellationToken = default)
         {
         {
             throw new Exception("The hot-reload feature requires .NET 6 or later.");
             throw new Exception("The hot-reload feature requires .NET 6 or later.");
         }
         }