-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathAsyncTimer.cs
More file actions
96 lines (85 loc) · 3.01 KB
/
AsyncTimer.cs
File metadata and controls
96 lines (85 loc) · 3.01 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
namespace ServiceControl.Infrastructure.BackgroundTasks
{
using System;
using System.Threading;
using System.Threading.Tasks;
public enum TimerJobExecutionResult
{
ScheduleNextExecution,
ExecuteImmediately,
DoNotContinueExecuting
}
public class TimerJob
{
public TimerJob(Func<CancellationToken, Task<TimerJobExecutionResult>> callback, TimeSpan due, TimeSpan interval, Action<Exception> errorCallback)
{
tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
task = Task.Run(async () =>
{
try
{
await Task.Delay(due, token);
while (!token.IsCancellationRequested)
{
try
{
var result = await callback(token);
if (result == TimerJobExecutionResult.DoNotContinueExecuting)
{
tokenSource.Cancel();
}
else if (result == TimerJobExecutionResult.ScheduleNextExecution)
{
await Task.Delay(interval, token);
}
//Otherwise execute immediately
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
break;
}
catch (Exception ex)
{
errorCallback(ex);
}
}
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
// no-op
}
}, CancellationToken.None);
}
public async Task Stop()
{
if (tokenSource == null)
{
return;
}
await tokenSource.CancelAsync();
tokenSource.Dispose();
if (task != null)
{
try
{
await task;
}
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
{
//NOOP
}
}
}
Task task;
CancellationTokenSource tokenSource;
}
public interface IAsyncTimer
{
TimerJob Schedule(Func<CancellationToken, Task<TimerJobExecutionResult>> callback, TimeSpan due, TimeSpan interval, Action<Exception> errorCallback);
}
public class AsyncTimer : IAsyncTimer
{
public TimerJob Schedule(Func<CancellationToken, Task<TimerJobExecutionResult>> callback, TimeSpan due, TimeSpan interval, Action<Exception> errorCallback) => new TimerJob(callback, due, interval, errorCallback);
}
}