-
Notifications
You must be signed in to change notification settings - Fork 59
/
RunAbleThread.cs
40 lines (34 loc) · 1.3 KB
/
RunAbleThread.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
using System.Threading;
/// <summary>
/// The superclass that you should derive from. It provides Start() and Stop() method and Running property.
/// It will start the thread to run Run() when you call Start().
/// </summary>
public abstract class RunAbleThread
{
private readonly Thread _runnerThread;
protected RunAbleThread()
{
// we need to create a thread instead of calling Run() directly because it would block unity
// from doing other tasks like drawing game scenes
_runnerThread = new Thread(Run);
}
protected bool Running { get; private set; }
/// <summary>
/// This method will get called when you call Start(). Programmer must implement this method while making sure that
/// this method terminates in a finite time. You can use Running property (which will be set to false when Stop() is
/// called) to determine when you should stop the method.
/// </summary>
protected abstract void Run();
public void Start()
{
Running = true;
_runnerThread.Start();
}
public void Stop()
{
Running = false;
// block main thread, wait for _runnerThread to finish its job first, so we can be sure that
// _runnerThread will end before main thread end
_runnerThread.Join();
}
}