-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathIntroCancellationToken.cs
More file actions
32 lines (28 loc) · 1.02 KB
/
IntroCancellationToken.cs
File metadata and controls
32 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
namespace BenchmarkDotNet.Samples;
/// <summary>
/// Demonstrates cooperative cancellation using [BenchmarkCancellation] attribute.
/// When a benchmark class has a property or field marked with [BenchmarkCancellation], BenchmarkDotNet automatically
/// injects the cancellation token, allowing benchmarks to check for cancellation during execution.
/// This is useful for long-running async benchmarks that should respond to Ctrl+C or other cancellation signals.
/// </summary>
public class IntroCancellationToken
{
[BenchmarkCancellation]
public CancellationToken CancellationToken { get; set; }
[Benchmark]
public async Task AsyncBenchmark()
{
for (int i = 0; i < 100; i++)
{
await DoWorkAsync(CancellationToken);
}
}
private async Task DoWorkAsync(CancellationToken cancellationToken)
{
// Simulate some async work
await Task.Delay(100, cancellationToken);
}
}