Exemple #1
0
 public void DealQueue(IBlockQueue <Task> _todoQueue, ThreadWorker _thread_worker, Func <bool> _FuncIsTimeOut, bool _isInMainThread = true)
 {
     while (true)
     {
         Task _task = null;
         if (!_isInMainThread)
         {
             _task = _todoQueue.Dequeue();
         }
         else
         {
             if (!_todoQueue.TryDequeue(out _task))
             {
                 if (_todoQueue == CurFrameTasks)
                 {
                     if (!TryDequeueWaitEqueueTasks(_todoQueue, out _task))
                     {
                         return;
                     }
                 }
                 else
                 {
                     if (_FuncIsTimeOut())
                     {
                         return;
                     }
                     if (!TryDequeueWaitEqueueTasks(_todoQueue, out _task))
                     {
                         return;
                     }
                 }
             }
         }
         _thread_worker.ResetThreadLoacalStorage();
         _task.threadLocalStorage = _thread_worker;
         if (!_task.IsFinished)
         {
             //处理还未完成的任务
             try {
                 DealTask(_task);
                 _task.MoveNextPhase();
                 //根据当前任务的状态派发到不同的队列中
                 var _queueType = _task.GetTargetQueueType();
                 var _nextQueue = GetQueueFromType(_queueType);
                 if (_isInMainThread)
                 {
                     //为了防止阻塞
                     if (!_nextQueue.TryEnqueue(_task))
                     {
                         AddWaitEnqueueTask(_nextQueue, _task);
                     }
                 }
                 else
                 {
                     _nextQueue.Enqueue(_task);
                 }
             } catch (System.Threading.ThreadInterruptedException) {
                 //正常现象
                 _task.IsInterrupted = true;
                 if (_isInMainThread)
                 {
                     NoBlockingEnqueue(_task);
                 }
                 else
                 {
                     BlockingEnqueue(MainThreadTasks, _task);
                 }
             } catch (System.Exception _e) {
                 _task.Exception = _e;
                 if (_isInMainThread)
                 {
                     NoBlockingEnqueue(_task);
                 }
                 else
                 {
                     BlockingEnqueue(MainThreadTasks, _task);
                 }
             }
             if (_FuncIsTimeOut != null && _FuncIsTimeOut())
             {
                 break;
             }
         }
         else
         {
             Debug.Assert(_isInMainThread, "LogicError: Callback Must in mainThread");
             //处理已经完成了的任务,进行回调
             try {
                 if (_task.IsInterrupted)
                 {
                     _task.OnInterrupted();
                 }
                 else
                 {
                     _task.OnFinished();
                 }
                 if (_FuncIsTimeOut != null && _FuncIsTimeOut())
                 {
                     break;
                 }
             } catch (Exception _e) {
                 //这里已经是处理完成的任务相关
                 Debug.LogError("处理任务完成回调抛出异常: " + _e + " _task " + _task.ToString());
             }
         }
     }
 }