/// <summary>
        /// 等待所有的<see cref="TaskUpdate"/>完成
        /// </summary>
        private void WaitTaskUpdate()
        {
            for (int iTask = 0; iTask < m_TaskItemsCache.Count; iTask++)
            {
                TaskUpdateItem taskItem = m_TaskItemsCache[iTask];
                taskItem.ManualResetEvent.WaitOne();

                BaseBehaviour iterBehaviour = taskItem.Behaviour;
                try
                {
                    MDebug.LogVerbose("Core"
                                      , $"Before execute {iterBehaviour.GetName()}.OnAfterTaskUpdate");

                    iterBehaviour.OnAfterTaskUpdate(taskItem.Output);

                    MDebug.LogVerbose("Core"
                                      , $"After execute {iterBehaviour.GetName()}.OnAfterTaskUpdate");
                }
                catch (Exception e)
                {
                    MDebug.LogError("Core"
                                    , $"Execute {iterBehaviour.GetName()}.OnAfterTaskUpdate Exception:{e.ToString()}");
                }

                m_TaskItemPool.Release(taskItem);
            }

            m_TaskItemsCache.Clear();
        }
Exemple #2
0
        /// <summary>
        /// 对<see cref="OnTaskUpdate_Thread"/>的一层封装
        ///     错误处理
        ///     处理<see cref="System.Threading.ManualResetEvent"/>
        /// </summary>
        internal void DoTaskUpdate_Thread(object state)
        {
            TaskUpdateItem taskItem = state as TaskUpdateItem;

            try
            {
                MDebug.LogVerbose("Core"
                                  , $"Before execute {GetName()}.OnTaskUpdate_Thread");

                taskItem.Output = OnTaskUpdate_Thread(taskItem.Input, taskItem.DeltaTime);

                MDebug.LogVerbose("Core"
                                  , $"After execute {GetName()}.OnTaskUpdate_Thread");
            }
            catch (Exception e)
            {
                MDebug.LogError("Core"
                                , $"Execute {GetName()}.OnTaskUpdate_Thread Exception:{e.ToString()}");
            }

            taskItem.ManualResetEvent.Set();
        }
        private void TaskUpdate(float deltaTime)
        {
            MDebug.Assert(m_TaskItemsCache.Count == 0, "Core", "m_TaskItemsCache.Count == 0");

            for (int iBehaviour = 0; iBehaviour < m_BehavioursForTraverse.Count; iBehaviour++)
            {
                BaseBehaviour iterBehaviour = m_BehavioursForTraverse[iBehaviour];
                if (!iterBehaviour.CanUpdate() ||
                    !iterBehaviour.HasFeature(FeatureFlag.TaskUpdate))
                {
                    continue;
                }

                TaskUpdateItem iterTaskItem = m_TaskItemPool.Alloc()
                                              .SetData(iterBehaviour, deltaTime);
                m_TaskItemsCache.Add(iterTaskItem);

                try
                {
                    MDebug.LogVerbose("Core"
                                      , $"Before execute {iterBehaviour.GetName()}.OnBeforeTastUpdate");

                    iterTaskItem.Input = iterBehaviour.OnBeforeTastUpdate();

                    MDebug.LogVerbose("Core"
                                      , $"After execute {iterBehaviour.GetName()}.OnBeforeTastUpdate");
                }
                catch (Exception e)
                {
                    MDebug.LogError("Core"
                                    , $"Execute {iterBehaviour.GetName()}.OnBeforeTastUpdate Exception:{e.ToString()}");
                }

                System.Threading.ThreadPool.QueueUserWorkItem(iterBehaviour.DoTaskUpdate_Thread, iterTaskItem);
            }
        }