@@ -371,9 +371,9 @@ static void ShowVersion()
371371
372372 static partial void ShowHelp(int helpId);
373373
374- static async Task RunWithFilterAsync(string commandName, string[] args, int commandDepth, int escapeIndex, ConsoleAppFilter invoker)
374+ static async Task RunWithFilterAsync(string commandName, string[] args, int commandDepth, int escapeIndex, ConsoleAppFilter invoker, CancellationToken cancellationToken )
375375 {
376- using var posixSignalHandler = PosixSignalHandler.Register(Timeout);
376+ using var posixSignalHandler = PosixSignalHandler.Register(Timeout, cancellationToken );
377377 try
378378 {
379379 await Task.Run(() => invoker.InvokeAsync(new ConsoleAppContext(commandName, args, null, commandDepth, escapeIndex), posixSignalHandler.Token)).WaitAsync(posixSignalHandler.TimeoutToken);
@@ -411,16 +411,16 @@ sealed class PosixSignalHandler : IDisposable
411411 PosixSignalRegistration? sigQuit;
412412 PosixSignalRegistration? sigTerm;
413413
414- PosixSignalHandler(TimeSpan timeout)
414+ PosixSignalHandler(TimeSpan timeout, CancellationToken cancellationToken )
415415 {
416- this.cancellationTokenSource = new CancellationTokenSource( );
416+ this.cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken );
417417 this.timeoutCancellationTokenSource = new CancellationTokenSource();
418418 this.timeout = timeout;
419419 }
420420
421- public static PosixSignalHandler Register(TimeSpan timeout)
421+ public static PosixSignalHandler Register(TimeSpan timeout, CancellationToken cancellationToken )
422422 {
423- var handler = new PosixSignalHandler(timeout);
423+ var handler = new PosixSignalHandler(timeout, cancellationToken );
424424
425425 Action<PosixSignalContext> handleSignal = handler.HandlePosixSignal;
426426
@@ -443,6 +443,7 @@ public void Dispose()
443443 sigInt?.Dispose();
444444 sigQuit?.Dispose();
445445 sigTerm?.Dispose();
446+ cancellationTokenSource.Dispose();
446447 timeoutCancellationTokenSource.Dispose();
447448 }
448449 }
@@ -482,10 +483,10 @@ public void UseFilter<T>() where T : ConsoleAppFilter { }
482483 partial void AddCore(string commandName, Delegate command);
483484
484485 [MethodImpl(MethodImplOptions.AggressiveInlining)]
485- partial void RunCore(string[] args);
486+ partial void RunCore(string[] args, CancellationToken cancellationToken );
486487
487488 [MethodImpl(MethodImplOptions.AggressiveInlining)]
488- partial void RunAsyncCore(string[] args, ref Task result);
489+ partial void RunAsyncCore(string[] args, CancellationToken cancellationToken, ref Task result);
489490
490491 partial void BuildAndSetServiceProvider();
491492
@@ -546,15 +547,14 @@ internal static partial class ConsoleApp
546547 internal partial class ConsoleAppBuilder
547548 {
548549 public void Run(string[] args) => Run(args, true);
550+ public void Run(string[] args, CancellationToken cancellationToken) => Run(args, true, cancellationToken);
549551
550- public Task RunAsync(string[] args) => RunAsync(args, true);
551-
552- public void Run(string[] args, bool disposeService)
552+ public void Run(string[] args, bool disposeService, CancellationToken cancellationToken = default)
553553 {
554554 BuildAndSetServiceProvider();
555555 try
556556 {
557- RunCore(args);
557+ RunCore(args, cancellationToken );
558558 }
559559 finally
560560 {
@@ -568,13 +568,16 @@ public void Run(string[] args, bool disposeService)
568568 }
569569 }
570570
571- public async Task RunAsync(string[] args, bool disposeService)
571+ public Task RunAsync(string[] args) => RunAsync(args, true);
572+ public Task RunAsync(string[] args, CancellationToken cancellationToken) => RunAsync(args, true, cancellationToken);
573+
574+ public async Task RunAsync(string[] args, bool disposeService, CancellationToken cancellationToken = default)
572575 {
573576 BuildAndSetServiceProvider();
574577 try
575578 {
576579 Task? task = null;
577- RunAsyncCore(args, ref task!);
580+ RunAsyncCore(args, cancellationToken, ref task!);
578581 if (task != null)
579582 {
580583 await task;
@@ -621,8 +624,9 @@ internal static partial class ConsoleApp
621624 internal partial class ConsoleAppBuilder
622625 {
623626 public void Run(string[] args) => Run(args, true, true, true);
627+ public void Run(string[] args, CancellationToken cancellationToken) => Run(args, true, true, true, cancellationToken);
624628
625- public void Run(string[] args, bool startHost, bool stopHost, bool disposeService)
629+ public void Run(string[] args, bool startHost, bool stopHost, bool disposeService, CancellationToken cancellationToken = default )
626630 {
627631 BuildAndSetServiceProvider();
628632 Microsoft.Extensions.Hosting.IHost? host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;
@@ -632,7 +636,7 @@ public void Run(string[] args, bool startHost, bool stopHost, bool disposeServic
632636 {
633637 host?.StartAsync().GetAwaiter().GetResult();
634638 }
635- RunCore(args);
639+ RunCore(args, cancellationToken );
636640 }
637641 finally
638642 {
@@ -651,8 +655,9 @@ public void Run(string[] args, bool startHost, bool stopHost, bool disposeServic
651655 }
652656
653657 public Task RunAsync(string[] args) => RunAsync(args, true, true, true);
658+ public Task RunAsync(string[] args, CancellationToken cancellationToken) => RunAsync(args, true, true, true, cancellationToken);
654659
655- public async Task RunAsync(string[] args, bool startHost, bool stopHost, bool disposeService)
660+ public async Task RunAsync(string[] args, bool startHost, bool stopHost, bool disposeService, CancellationToken cancellationToken = default )
656661 {
657662 BuildAndSetServiceProvider();
658663 Microsoft.Extensions.Hosting.IHost? host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;
@@ -663,7 +668,7 @@ public async Task RunAsync(string[] args, bool startHost, bool stopHost, bool di
663668 await host?.StartAsync();
664669 }
665670 Task? task = null;
666- RunAsyncCore(args, ref task!);
671+ RunAsyncCore(args, cancellationToken, ref task!);
667672 if (task != null)
668673 {
669674 await task;
0 commit comments