void ThreadRun()
 {
     while (state == FogOfWarThreadState.Running)
     {
         if (task != null)
         {
             try
             {
                 task.Run();
             }
             catch (System.Exception e)
             {
                 Debug.LogException(e);
             }
             task = null;
         }
         else
         {
             task = threadPool.RequestNewTask(this);
             if (task == null)
             {
                 Thread.Sleep(threadPool.sleepTime);
             }
         }
     }
     state   = FogOfWarThreadState.Stopped;
     _thread = null;
 }
        public void Run(FogOfWarThreadTask task)
        {
            // add to any waiting threads
            for (int i = maxThreads; i < _threads.Count; ++i)
            {
                if (_threads[i].state == FogOfWarThreadState.Running && _threads[i].isWaiting)
                {
                    _threads[i].Run(task);
                    return;
                }
            }

            // create thread
            if (_threads.Count < maxThreads)
            {
                FogOfWarThread newthread = new FogOfWarThread(this);
                _threads.Add(newthread);
                newthread.Run(task);
                return;
            }

            // no available threads, so just add it to the queue
            lock (_taskQueue)
                _taskQueue.Add(task);
        }
 public void Run(FogOfWarThreadTask newtask)
 {
     if (task != null)
     {
         Debug.LogError("FogOfWarThread is trying to start task before another ends!");
     }
     else
     {
         task = newtask;
     }
 }
 internal FogOfWarThreadTask RequestNewTask(FogOfWarThread thread)
 {
     lock (_taskQueue)
     {
         if (_taskQueue.Count > 0)
         {
             FogOfWarThreadTask newtask = _taskQueue[_taskQueue.Count - 1];
             _taskQueue.RemoveAt(_taskQueue.Count - 1);
             return(newtask);
         }
     }
     return(null);
 }