Skip to content

Commit 4f3ec9c

Browse files
committed
sample code of RunAsync(cancellationToken)
1 parent 5e4bb9f commit 4f3ec9c

2 files changed

Lines changed: 58 additions & 22 deletions

File tree

ReadMe.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,16 +768,35 @@ The hooking behavior using `PosixSignalRegistration` is determined by the presen
768768
In the case of `Run/RunAsync` from `ConsoleAppBuilder`, you can also pass a CancellationToken. This is combined with PosixSignalRegistration and passed to each method. This makes it possible to cancel at any arbitrary timing.
769769

770770
```csharp
771-
var cancellationTokenSource = new CancellationTokenSource();
771+
// Create a CancellationTokenSource that will be cancelled when 'Q' is pressed.
772+
var cts = new CancellationTokenSource();
773+
_ = Task.Run(() =>
774+
{
775+
while (Console.ReadKey().Key != ConsoleKey.Q) ;
776+
Console.WriteLine();
777+
cts.Cancel();
778+
});
772779

773780
var app = ConsoleApp.Create();
774781

775-
app.Add("", (CancellationToken cancellationToken) =>
782+
app.Add("", async (CancellationToken cancellationToken) =>
776783
{
777-
// do anything...
784+
// CancellationToken will be triggered when 'Q' is pressed or Ctrl+C(SIGINT/SIGTERM/SIGKILL) is sent.
785+
try
786+
{
787+
for (int i = 0; i < 10; i++)
788+
{
789+
Console.WriteLine($"Running main task iteration {i + 1}/10. Press 'Q' to quit.");
790+
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
791+
}
792+
}
793+
catch (OperationCanceledException)
794+
{
795+
Console.WriteLine("Main task was cancelled.");
796+
}
778797
});
779798

780-
await app.RunAsync(args, cancellationTokenSource.Token); // pass external CancellationToken
799+
await app.RunAsync(args, cts.Token); // pass external CancellationToken
781800
```
782801

783802
Exit Code

sandbox/GeneratorSandbox/Program.cs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,36 @@
66

77
// args = "some-command hello --global-flag flag-value -- more args here".Split(" ");
88

9-
var app = ConsoleApp.Create()
10-
// setup DryIoc as the DI container
11-
.ConfigureContainer(new DryIocServiceProviderFactory(), container =>
12-
{
13-
container.Register<MyService>();
14-
});
159

16-
app.UseFilter<MyFilter>();
17-
app.Add("", ([FromServices] MyService service) => service.Test());
10+
// Create a CancellationTokenSource that will be cancelled when 'Q' is pressed.
11+
var cts = new CancellationTokenSource();
12+
_ = Task.Run(() =>
13+
{
14+
while (Console.ReadKey().Key != ConsoleKey.Q) ;
15+
Console.WriteLine();
16+
cts.Cancel();
17+
});
1818

19-
app.Run(args);
19+
var app = ConsoleApp.Create();
2020

21+
app.Add("", async (CancellationToken cancellationToken) =>
22+
{
23+
// CancellationToken will be triggered when 'Q' is pressed or Ctrl+C(SIGINT/SIGTERM/SIGKILL) is sent.
24+
try
25+
{
26+
for (int i = 0; i < 10; i++)
27+
{
28+
Console.WriteLine($"Running main task iteration {i + 1}/10. Press 'Q' to quit.");
29+
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
30+
}
31+
}
32+
catch (OperationCanceledException)
33+
{
34+
Console.WriteLine("Main task was cancelled.");
35+
}
36+
});
2137

38+
await app.RunAsync(args, cts.Token); // pass external CancellationToken
2239

2340
//app.UseFilter<MyFilter>();
2441
// app.Run(["cmd", "test"]);
@@ -37,12 +54,12 @@ public override async Task InvokeAsync(ConsoleAppContext context, CancellationTo
3754
}
3855
}
3956

40-
[RegisterCommands("cmd")]
41-
public class MyCommand
42-
{
43-
[Command("test")]
44-
public int Test()
45-
{
46-
return 1;
47-
}
48-
}
57+
//[RegisterCommands("cmd")]
58+
//public class MyCommand
59+
//{
60+
// [Command("test")]
61+
// public int Test()
62+
// {
63+
// return 1;
64+
// }
65+
//}

0 commit comments

Comments
 (0)