using System.Threading; namespace FileLoaders { /// /// 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(). /// public abstract class RunnableThread { private readonly Thread _runnerThread; protected RunnableThread() { // 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; } /// /// 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. /// 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(); } } }