Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AsyncProcessors.cs
2using System.Threading.Tasks;
3
5{
9 public static class AsyncProcessors
10 {
11 private static readonly LinkedList<IAsyncProcessor> activeProcessors = new LinkedList<IAsyncProcessor>();
12
18 internal static LinkedListNode<IAsyncProcessor> RegisterProcessor(IAsyncProcessor Processor)
19 {
20 lock (activeProcessors)
21 {
22 return activeProcessors.AddLast(Processor);
23 }
24 }
25
30 internal static void UnregisterProcessor(LinkedListNode<IAsyncProcessor> Node)
31 {
32 if (!(Node is null))
33 {
34 lock (activeProcessors)
35 {
36 if (!(Node.List is null))
37 activeProcessors.Remove(Node);
38 }
39 }
40 }
41
47 {
48 lock (activeProcessors)
49 {
50 IAsyncProcessor[] Result = new IAsyncProcessor[activeProcessors.Count];
51 activeProcessors.CopyTo(Result, 0);
52 return Result;
53 }
54 }
55
59 public static bool HasActiveProcessors
60 {
61 get
62 {
63 lock (activeProcessors)
64 {
65 return !(activeProcessors.First is null);
66 }
67 }
68 }
69
74 {
75 return CloseAllProcessorsForTermination(false, 0);
76 }
77
84 public static Task CloseAllProcessorsForTermination(bool WaitForCompletion)
85 {
86 return CloseAllProcessorsForTermination(WaitForCompletion, int.MaxValue);
87 }
88
97 public static async Task CloseAllProcessorsForTermination(bool WaitForCompletion, int Timeout)
98 {
99 IAsyncProcessor[] Processors = GetActiveProcessors();
100 int i, c = Processors.Length;
101 Task[] Tasks = new Task[c];
102
103 for (i = 0; i < c; i++)
104 Tasks[i] = Processors[i].CloseForTermination(WaitForCompletion, Timeout);
105
106 if (WaitForCompletion)
107 await Task.WhenAll(Tasks);
108 }
109 }
110}
Maintains a record of active processors.
static Task CloseAllProcessorsForTermination(bool WaitForCompletion)
Closes all active processors for new items. If WaitForCompletion is true, the method waits for queue...
static IAsyncProcessor[] GetActiveProcessors()
Gets an array of all active processors.
static async Task CloseAllProcessorsForTermination(bool WaitForCompletion, int Timeout)
Closes all active processors for new items. If WaitForCompletion is true, the method waits for queue...
static Task CloseAllProcessorsForTermination()
Closes all active processors for new items.
static bool HasActiveProcessors
If there are active processors.
Interface for AsyncProcessor<T> instances.