-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBgThread.cs
255 lines (212 loc) · 8.79 KB
/
BgThread.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace BTD_Backend
{
/// <summary>
/// This delegate is used to pass functions into the ThreadQueue
/// </summary>
public delegate void funcDelegate();
/// <summary>
/// Background thread class. Used to manage threading in the application
/// </summary>
public class BgThread
{
#region Properties
private static BgThread instance;
/// <summary>
/// Singleton instance of this class
/// </summary>
public static BgThread Instance
{
get
{
if (instance == null)
instance = new BgThread();
return instance;
}
}
/// <summary>
/// The thread this class uses to do threading
/// </summary>
public static Thread ThreadInstance { get; private set; }
/// <summary>
/// A queue of threads that need to be ran
/// </summary>
public static Queue<Thread> ThreadQueue { get; private set; }
/// <summary>
/// A queue of threads that need to be ran. Used as a temp queue for adding threads to the front of the queue.
/// </summary>
private static Queue<Thread> addToFrontThreadQueue;
private static Queue<Thread> AddToFrontThreadQueue
{
get
{
if (addToFrontThreadQueue == null)
addToFrontThreadQueue = new Queue<Thread>();
return addToFrontThreadQueue;
}
set { addToFrontThreadQueue = value; }
}
#endregion
/// <summary>
/// Check if the background thread is in use or not
/// </summary>
/// <returns>Whether or not the background thread is in use</returns>
public static bool IsThreadRunning() => !(ThreadInstance == null || !ThreadInstance.IsAlive);
/// <summary>
/// Add a function to the ThreadQueue. It will execute the thread immediately if the thread Instance for this class isn't running
/// </summary>
/// <param name="func">The function to be added to the TheadQueue and run as a thread</param>
public static void AddToQueue(funcDelegate func, ApartmentState threadType = ApartmentState.Unknown, bool join = false) => AddToQueue(new Thread(() => func()), threadType, join);
/// <summary>
/// Add a thread to the ThreadQueue. It will execute the thread immediately if the thread Instance for this class isn't running
/// </summary>
/// <param name="thread">Thread to be added to the ThreadQueue</param>
public static void AddToQueue(Thread thread, ApartmentState threadType = ApartmentState.Unknown, bool join = false)
{
if (ThreadQueue == null)
ThreadQueue = new Queue<Thread>();
thread.SetApartmentState(threadType);
ThreadQueue.Enqueue(thread);
ThreadingEventArgs args = new ThreadingEventArgs();
args.Thread = thread;
Instance.ItemAddedToThreadQueue(args);
if (!IsThreadRunning())
Instance.ExecuteQueue(join);
}
/// <summary>
/// Add a list of threads to the ThreadQueue. It will execute them immediately if the thread Instance for this class isn't running
/// </summary>
/// <param name="threads"></param>
public static void AddToQueue(List<Thread> threads, ApartmentState threadType = ApartmentState.Unknown, bool join = false)
{
if (threads == null || threads.Count() <= 0)
{
Log.Output("Error! Tried adding an empty thread list to the Thread Queue");
return;
}
foreach (var thread in threads)
AddToQueue(thread, threadType, join);
}
/*public static void AddToFrontOfQueue(funcDelegate func) => AddToFrontOfQueue(new Thread(() => func()));
public static void AddToFrontOfQueue(Thread thread)
{
if (AddToFrontThreadQueue == null)
AddToFrontThreadQueue = new Queue<Thread>();
var tempQueue = new Queue<Thread>();
tempQueue = AddToFrontThreadQueue;
AddToFrontThreadQueue = new Queue<Thread>();
AddToFrontThreadQueue.Enqueue(thread);
foreach (var tempT in tempQueue)
AddToFrontThreadQueue.Enqueue(tempT);
}*/
int runningThreads = 0;
/// <summary>
/// Run the first thread in the ThreadQueue
/// </summary>
private void RunThread(bool nullifyThreadInst = false)
{
ThreadQueue.Peek().IsBackground = true;
ThreadQueue.Peek().Start();
runningThreads++;
if (runningThreads >= 3)
{
ThreadQueue.Peek().Join();
runningThreads--;
}
ThreadingEventArgs removeArgs = new ThreadingEventArgs();
removeArgs.Thread = ThreadQueue.Peek();
ThreadQueue.Dequeue();
ItemRemovedFromThreadQueue(removeArgs);
}
/// <summary>
/// Excecutes threads on queue until none are left
/// </summary>
/// <param name="join">Wait for ThreadInstance to finish before re-joining with the main thread?</param>
private void RunThreads(bool join = false)
{
if (ThreadQueue.Count() <= 0)
return;
ThreadInstance = new Thread(() =>
{
ThreadingEventArgs args = new ThreadingEventArgs();
args.ThreadQueue = ThreadQueue;
OnThreadQueueStarted(args);
while (ThreadQueue.Count > 0)
RunThread();
OnThreadQueueFinished(null);
});
ThreadInstance.IsBackground = true;
ThreadInstance.Start();
if (join)
ThreadInstance.Join();
}
/// <summary>
/// Calls RunThreads to excecutes threads on queue until none are left
/// </summary>
public void ExecuteQueue(bool join = false) => RunThreads(join);
/// <summary>
/// Get the background thread instance of the class
/// </summary>
/// <returns>the background thread instance</returns>
public static Thread GetThreadInst() => ThreadInstance;
/// <summary>
/// Get the instance of BgThread class
/// </summary>
/// <returns></returns>
public static BgThread GetInstance() => Instance;
#region Events
public static event EventHandler<ThreadingEventArgs> ThreadQueueFinished;
public static event EventHandler<ThreadingEventArgs> ThreadQueueStarted;
public static event EventHandler<ThreadingEventArgs> ThreadQueueItemAdded;
public static event EventHandler<ThreadingEventArgs> ThreadQueueItemRemoved;
public class ThreadingEventArgs : EventArgs
{
public Queue<Thread> ThreadQueue { get; set; }
public Thread Thread { get; set; }
}
/// <summary>
/// Event fired when a thread has been added to the ThreadQueue
/// </summary>
/// <param name="e">Passes current Queue as argument</param>
public void OnThreadQueueStarted(ThreadingEventArgs e)
{
EventHandler<ThreadingEventArgs> handler = ThreadQueueStarted;
if (handler != null)
handler(this, e);
}
/// <summary>
/// Event fired when ThreadQueue finished executing all threads
/// </summary>
/// <param name="e">Can be null</param>
public void OnThreadQueueFinished(ThreadingEventArgs e)
{
EventHandler<ThreadingEventArgs> handler = ThreadQueueFinished;
if (handler != null)
handler(this, e);
}
/// <summary>
/// Event fired when an item is added to the ThreadQueue
/// </summary>
/// <param name="e">The thread that was added to the queue is passed as an arg</param>
public void ItemAddedToThreadQueue(ThreadingEventArgs e)
{
EventHandler<ThreadingEventArgs> handler = ThreadQueueItemAdded;
if (handler != null)
handler(this, e);
}
/// <summary>
/// Event fired when an item is removed from ThreadQueue
/// </summary>
/// <param name="e">The thread that was removed from queue is passed as arg</param>
public void ItemRemovedFromThreadQueue(ThreadingEventArgs e)
{
EventHandler<ThreadingEventArgs> handler = ThreadQueueItemRemoved;
if (handler != null)
handler(this, e);
}
#endregion
}
}