Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ThreadExecutor.cs
1using System;
2using System.Collections.Generic;
3using System.Threading;
4using System.Threading.Tasks;
5using Waher.Events;
6
8{
14 public delegate ReturnType Callback0<ReturnType>();
15
23 public delegate ReturnType Callback1<ReturnType, Arg1Type>(Arg1Type Arg1);
24
34 public delegate ReturnType Callback2<ReturnType, Arg1Type, Arg2Type>(Arg1Type Arg1, Arg2Type Arg2);
35
47 public delegate ReturnType Callback3<ReturnType, Arg1Type, Arg2Type, Arg3Type>(Arg1Type Arg1, Arg2Type Arg2, Arg3Type Arg3);
48
53 public class ThreadExecutor : IDisposable
54 {
55 private readonly ManualResetEvent terminate = new ManualResetEvent(false);
56 private readonly AutoResetEvent newTask = new AutoResetEvent(false);
57 private readonly LinkedList<ISyncTask> tasks = new LinkedList<ISyncTask>();
58 private Thread thread;
59 private bool terminating = false;
60 private bool terminated = false;
61 private bool idle = true;
62 private bool working = false;
63
69 {
70 this.thread = new Thread(this.Executor);
71 this.thread.Start();
72 }
73
77 public void Dispose()
78 {
79 if (!this.terminating && !this.terminated)
80 {
81 this.terminating = true;
82 this.terminate?.Set();
83 }
84 }
85
89 public bool Terminating => this.terminating;
90
94 public bool Terminated => this.terminated;
95
99 public bool Idle => this.idle;
100
104 public bool Working => this.working;
105
106 private void Add(ISyncTask Task)
107 {
108 if (this.terminating || this.terminated)
109 throw new InvalidOperationException("Thread executer is being terminated or has been terminated.");
110
111 lock (this.tasks)
112 {
113 this.tasks.AddLast(Task);
114 this.newTask.Set();
115 }
116 }
117
124 public Task<ReturnType> Execute<ReturnType>(Callback0<ReturnType> Callback)
125 {
126 SyncTask0<ReturnType> Result = new SyncTask0<ReturnType>(Callback);
127 this.Add(Result);
128 return Result.WaitAsync();
129 }
130
140 Arg1Type Arg1)
141 {
143 this.Add(Result);
144 return Result.WaitAsync();
145 }
146
158 Callback2<ReturnType, Arg1Type, Arg2Type> Callback, Arg1Type Arg1, Arg2Type Arg2)
159 {
161 Callback, Arg1, Arg2);
162 this.Add(Result);
163 return Result.WaitAsync();
164 }
165
179 Callback3<ReturnType, Arg1Type, Arg2Type, Arg3Type> Callback, Arg1Type Arg1, Arg2Type Arg2,
180 Arg3Type Arg3)
181 {
183 new SyncTask3<ReturnType, Arg1Type, Arg2Type, Arg3Type>(Callback, Arg1, Arg2, Arg3);
184 this.Add(Result);
185 return Result.WaitAsync();
186 }
187
188 private void Executor()
189 {
190 try
191 {
192 WaitHandle[] Handles = new WaitHandle[] { this.newTask, this.terminate };
193 ISyncTask ToExecute;
194 bool Continue = true;
195
196 while (Continue)
197 {
198 switch (WaitHandle.WaitAny(Handles, 1000))
199 {
200 case 0:
201 do
202 {
203 lock (this.tasks)
204 {
205 if (this.tasks.First is null)
206 ToExecute = null;
207 else
208 {
209 ToExecute = this.tasks.First.Value;
210 this.tasks.RemoveFirst();
211 }
212 }
213
214 if (!(ToExecute is null))
215 {
216 try
217 {
218 ToExecute.Execute();
219 }
220 catch (Exception ex)
221 {
222 Log.Exception(ex);
223 }
224 }
225 }
226 while (!(ToExecute is null));
227 break;
228
229 case 1:
230 Continue = false;
231 break;
232 }
233 }
234 }
235 catch (Exception ex)
236 {
237 Log.Exception(ex);
238 }
239 finally
240 {
241 this.terminating = false;
242 this.terminated = true;
243 this.idle = false;
244 this.working = false;
245 this.thread = null;
246
247 this.terminate.Dispose();
248 this.newTask.Dispose();
249 }
250 }
251 }
252}
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
Task with zero arguments to be synchronized.
Definition: SyncTask0.cs:11
Task< ReturnType > WaitAsync()
Waits for the task to complete.
Task with one argument to be synchronized.
Definition: SyncTask1.cs:12
Task< ReturnType > WaitAsync()
Waits for the task to complete.
Task with one argument to be synchronized.
Definition: SyncTask2.cs:13
Task< ReturnType > WaitAsync()
Waits for the task to complete.
Task with one argument to be synchronized.
Definition: SyncTask3.cs:14
Task< ReturnType > WaitAsync()
Waits for the task to complete.
Class that executes tasks from the same the same thread, and that provides an asynchronous interface ...
bool Terminating
If execution thread is terminating.
Task< ReturnType > Execute< ReturnType, Arg1Type, Arg2Type >(Callback2< ReturnType, Arg1Type, Arg2Type > Callback, Arg1Type Arg1, Arg2Type Arg2)
Executes a task with one argument.
Task< ReturnType > Execute< ReturnType >(Callback0< ReturnType > Callback)
Executes a task with no arguments.
ThreadExecutor()
Class that executes tasks from the same the same thread, and that provides an asynchronous interface ...
bool Terminated
If execution thread has terminated.
Task< ReturnType > Execute< ReturnType, Arg1Type >(Callback1< ReturnType, Arg1Type > Callback, Arg1Type Arg1)
Executes a task with one argument.
Task< ReturnType > Execute< ReturnType, Arg1Type, Arg2Type, Arg3Type >(Callback3< ReturnType, Arg1Type, Arg2Type, Arg3Type > Callback, Arg1Type Arg1, Arg2Type Arg2, Arg3Type Arg3)
Executes a task with one argument.
void Dispose()
Disposes of the object and terminates the thread executor.
Interface for tasks to be synckronized.
Definition: ISyncTask.cs:7
delegate ReturnType Callback3< ReturnType, Arg1Type, Arg2Type, Arg3Type >(Arg1Type Arg1, Arg2Type Arg2, Arg3Type Arg3)
Delegate to methods of three parameters and a given return type.
delegate ReturnType Callback1< ReturnType, Arg1Type >(Arg1Type Arg1)
Delegate to methods of one parameter and a given return type.
delegate ReturnType Callback0< ReturnType >()
Delegate to methods of zero parameters and a given return type.
delegate ReturnType Callback2< ReturnType, Arg1Type, Arg2Type >(Arg1Type Arg1, Arg2Type Arg2)
Delegate to methods of two parameters and a given return type.