Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MqTasks.cs
1using System;
2using System.Collections.Generic;
3using System.Threading;
4
6{
10 internal static class MqTasks
11 {
12 private static readonly ManualResetEvent terminating = new ManualResetEvent(false);
13 private static readonly LinkedList<MqTaskThread> threads = new LinkedList<MqTaskThread>();
14 private static bool terminated = false;
15
16 static MqTasks()
17 {
18 Model.OnFinalizing += Model_OnFinalizing;
19 }
20
21 private static void Model_OnFinalizing(object sender, EventArgs e)
22 {
23 terminated = true;
24 terminating.Set();
25 }
26
27 internal static void ExecuteTask(MqTask Item)
28 {
29 if (terminated)
30 return;
31
32 MqTaskThread T;
33
34 lock (threads)
35 {
36 if (threads.First is null)
37 T = null;
38 else
39 {
40 T = threads.First.Value;
41 threads.RemoveFirst();
42 }
43 }
44
45 if (T is null)
46 T = new MqTaskThread(terminating);
47
48 T.Execute(Item);
49 }
50
51 internal static void Idle(MqTaskThread Thread)
52 {
53 lock (threads)
54 {
55 threads.AddLast(Thread);
56 }
57 }
58 }
59}