-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomTaskScheduler.cs
122 lines (102 loc) · 3.78 KB
/
CustomTaskScheduler.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using Serilog;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Assignment.CustomTreadPoolAndTaskScheduler
{
public class CustomTaskScheduler : TaskScheduler, IDisposable
{
private readonly ILogger _logger;
private readonly ICustomThreadPool _customThreadPool;
private readonly BlockingCollection<Task> _tasks;
private bool _disposed;
private Thread _schedulerThread;
private readonly CancellationTokenSource cancellationTokenSource;
public CustomTaskScheduler(ILogger logger,
ICustomThreadPool customThreadPool)
{
_logger = logger;
_customThreadPool = customThreadPool;
_disposed = false;
_tasks = new BlockingCollection<Task>();
cancellationTokenSource = new CancellationTokenSource();
}
protected override IEnumerable<Task> GetScheduledTasks()
{
return _tasks.ToArray();
}
protected override void QueueTask(Task task)
{
if (task == null) return;
_logger.Information("One task has arrived into Queue Task.");
if (_schedulerThread == null)
{
_schedulerThread = new Thread(() => ScheduleTaskOverThreadPool(cancellationTokenSource.Token))
{
IsBackground = true
};
_schedulerThread.Start();
}
_tasks.Add(task);
_logger.Information("The task has been added into the collection.");
}
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
if (task == null) return false;
if (taskWasPreviouslyQueued)
return false;
if ((task.IsCompleted == false ||
task.Status == TaskStatus.Running ||
task.Status == TaskStatus.WaitingToRun ||
task.Status == TaskStatus.WaitingForActivation))
{
return false;
}
else
{
return true;
}
}
private void ScheduleTaskOverThreadPool(CancellationToken token)
{
if (token.IsCancellationRequested) return;
_logger.Information("Start scheduling.");
foreach (var task in _tasks.GetConsumingEnumerable())
{
if (token.IsCancellationRequested) break;
_customThreadPool.StartAction(() => TryExecuteTask(task));
}
_logger.Information("Done scheduling.");
}
public void Dispose()
{
_logger.Information("The Dispose is called.");
Dispose(true);
GC.SuppressFinalize(this);
_logger.Information("The Dispose is completed.");
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
cancellationTokenSource.Cancel();
_logger.Information("Disposing..........");
// Indicate that no new tasks will be coming in
_tasks.CompleteAdding();
// _schedulerThread.Abort()
// The Thread.Abort method is not supported in .NET Core. If you need to terminate the execution of third - party code
// forcibly in .NET Core, run it in the separate process and use Process.Kill.
_customThreadPool.Dispose();
_schedulerThread.Join();
_tasks.Dispose();
cancellationTokenSource.Dispose();
}
_disposed = true;
}
}
}