-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRuntimeSampleTask.cs
More file actions
47 lines (39 loc) · 1.54 KB
/
RuntimeSampleTask.cs
File metadata and controls
47 lines (39 loc) · 1.54 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
namespace KK.DotNet.BackgroundTasks.Scheduled.Sample.Web.Backgroundservices
{
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
using KK.DotNet.BackgroundTasks.Scheduled;
public class RuntimeSampleTask : IScheduledTask
{
private readonly ILogger<RuntimeSampleTask> logger;
public RuntimeSampleTask(
IScheduledTaskOptions<RuntimeSampleTask> options,
ILogger<RuntimeSampleTask> logger
)
{
this.Options = options;
this.logger = logger;
}
public IScheduledTaskOptions<IScheduledTask> Options { get; }
public async Task ExecuteAsync(CancellationToken stoppingToken)
{
this.logger.LogDebug("Runtime Sample Task started");
_ = stoppingToken.Register(() => this.logger.LogDebug("Runtime Sample Task forced stopping."));
var runCount = 0;
while (runCount < 5)
{
if (stoppingToken.IsCancellationRequested)
{
// Task was killed from outside.
this.logger.LogDebug($"{System.DateTime.Now} Runtime Sample Task cancellation requested!");
return;
}
this.logger.LogDebug($"{System.DateTime.Now} Runtime Sample Task loop {runCount}");
await Task.Delay(1000, stoppingToken);
runCount++;
}
this.logger.LogDebug("Runtime Sample Task is finished");
}
}
}