-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadedTask.cs
54 lines (48 loc) · 1.41 KB
/
ThreadedTask.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
using System.Threading;
namespace ShockwaveGORN
{
/// <summary>Handles Multi-Threaded Tasking</summary>
public abstract class ThreadedTask
{
private Thread thread;
internal bool IsAlive()
=> thread?.IsAlive ?? false;
/// <summary>Initializes the Task's Thread</summary>
/// <returns>If the initialization was successful or not.</returns>
public bool BeginInit()
{
if (!BeginInitInternal())
return false;
RunThread();
return true;
}
internal abstract bool BeginInitInternal();
/// <summary>Aborts the Task's Thread</summary>
/// <returns>If the abortion was successful or not.</returns>
public bool EndInit()
{
if (!EndInitInternal())
return false;
KillThread();
return true;
}
internal abstract bool EndInitInternal();
internal abstract void WithinThread();
private void RunThread()
{
if (IsAlive())
KillThread();
thread = new Thread(WithinThread);
thread.Start();
}
private void KillThread()
{
if (!IsAlive())
return;
#pragma warning disable SYSLIB0006
thread.Abort();
#pragma warning restore SYSLIB0006
thread = null;
}
}
}