-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskService.cs
56 lines (52 loc) · 1.53 KB
/
TaskService.cs
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
using System;
using System.Threading;
using System.Threading.Tasks;
using Common.Logging;
namespace OplogTail
{
/// <summary>
/// Sub service that runs in a task
/// </summary>
public abstract class TaskService : DefaultService
{
private static readonly ILog Log = LogManager.GetLogger<TaskService>();
private Task _task;
protected TaskService()
{
_task = new Task(DoStart, CancelTokenSource.Token, TaskCreationOptions.LongRunning);
}
public override void Start()
{
_task.Start();
}
public override void Stop()
{
CancelTokenSource.Cancel();
_task = _task
.ContinueWith(
task =>
{
if (task.Exception != null)
{
task.Exception.Handle(
exception =>
{
Log.Error(exception.Message, exception);
return true;
});
}
},
CancellationToken.None,
TaskContinuationOptions.None,
TaskScheduler.Default);
try
{
_task.Wait();
}
catch (AggregateException ex)
{
Log.ErrorFormat(ex.Message, ex);
}
}
}
}