-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathFixedRateScheduledTask.cs
More file actions
52 lines (45 loc) · 1.63 KB
/
FixedRateScheduledTask.cs
File metadata and controls
52 lines (45 loc) · 1.63 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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
namespace DotNetty.Common.Concurrency
{
sealed class FixedRateScheduledTask : ScheduledTask
{
readonly Action action;
public FixedRateScheduledTask(AbstractScheduledEventExecutor executor, IRunnable action, PreciseTimeSpan deadline, PreciseTimeSpan period)
: this(executor, action.Run, deadline, period)
{
}
public FixedRateScheduledTask(AbstractScheduledEventExecutor executor, Action action, PreciseTimeSpan deadline, PreciseTimeSpan period)
: base(executor, deadline, new TaskCompletionSource())
{
if (period.Ticks <= 0)
throw new ArgumentException("period: 0 (expected: != 0)");
this.Period = period;
this.action = action;
}
public PreciseTimeSpan Period { get; }
protected override void Execute() => this.action();
public override void Run()
{
try
{
this.Execute();
if (!Executor.IsShutdown)
{
this.Deadline = PreciseTimeSpan.FromTicks(Deadline.Ticks + Period.Ticks);
this.Executor.Schedule(this);
}
else
{
this.Promise.TryComplete();
}
}
catch (Exception ex)
{
// todo: check for fatal
this.Promise.TrySetException(ex);
}
}
}
}