Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MqTaskThread.cs
1using System;
2using System.Collections.Generic;
3using System.Threading;
4using Waher.Events;
5
7{
11 internal class MqTaskThread
12 {
13 private readonly Thread thread;
14 private readonly AutoResetEvent newItem;
15 private readonly LinkedList<MqTask> tasks = new LinkedList<MqTask>();
16 private readonly WaitHandle[] waitHandles;
17
18 public MqTaskThread(ManualResetEvent Terminating)
19 {
20 this.newItem = new AutoResetEvent(false);
21 this.waitHandles = new WaitHandle[] { this.newItem, Terminating };
22
23 this.thread = new Thread(this.TaskExecutor)
24 {
25 Name = "MQ Async Thread",
26 Priority = ThreadPriority.BelowNormal
27 };
28
29 this.thread.Start();
30 }
31
32 public void Execute(MqTask Task)
33 {
34 lock (tasks)
35 {
36 this.tasks.AddLast(Task);
37 }
38
39 this.newItem.Set();
40 }
41
42 private void TaskExecutor()
43 {
44 try
45 {
46 MqTask Task;
47 bool Working = true;
48
49 while (true)
50 {
51 switch (WaitHandle.WaitAny(this.waitHandles, 1000))
52 {
53 case 0: // New item
54 Working = true;
55 while (true)
56 {
57 lock (tasks)
58 {
59 if (tasks.First is null)
60 break;
61 else
62 {
63 Task = tasks.First.Value;
64 tasks.RemoveFirst();
65 }
66 }
67
68 if (Task.DoWork())
69 {
70 lock (tasks)
71 {
72 tasks.AddLast(Task);
73 }
74 }
75 }
76 break;
77
78 case 1: // Terminating
79 return;
80
81 default:
82 if (Working)
83 {
84 Working = false;
85 MqTasks.Idle(this);
86 }
87 break;
88 }
89 }
90 }
91 catch (Exception ex)
92 {
93 Log.Exception(ex);
94 }
95 }
96 }
97}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Priority
Mail priority
Definition: Priority.cs:7