Ejemplo n.º 1
0
    //替换任务
    public bool ReplaceFrameTask(Action <int> callback, int delay, int count = 1)
    {
        //新建这个task
        PETrameTask newTask = new PETrameTask(tID, callback, frameCounter + delay, delay, count);
        bool        isRep   = false;

        //去任务列表中寻找这个tid的任务,把它替换掉
        for (int i = 0; i < taskFrameList.Count; i++)
        {
            if (taskFrameList[i].tID == tID)
            {
                taskFrameList[i] = newTask;
                isRep            = true;
                break;
            }
        }
        //没有找到的话就去缓存列表里面找
        if (!isRep)
        {
            for (int i = 0; i < tmpFrameList.Count; i++)
            {
                if (tmpFrameList[i].tID == tID)
                {
                    tmpFrameList[i] = newTask;
                    isRep           = true;
                    break;
                }
            }
        }
        return(isRep);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 删除定时任务
    /// </summary>
    /// <param name="tID">任务ID</param>
    /// <returns></returns>
    public bool DelFrameTask(int tID)
    {
        //是否已经找到这个任务
        bool exist = false;

        //先去任务列表中寻找这个任务
        for (int i = 0; i < taskFrameList.Count; i++)
        {
            PETrameTask task = taskFrameList[i];
            //任务列表中找到了 就在列表中删除这个任务
            if (task.tID == tID)
            {
                taskFrameList.RemoveAt(i);
                //删除任务后,再从tid合集中把这个tid删除,让tid空闲出来,给其他的任务使用
                for (int j = 0; j < tIDList.Count; j++)
                {
                    if (tIDList[j] == tID)
                    {
                        tIDList.RemoveAt(j);
                        break;
                    }
                }
                exist = true;
                break;
            }
        }
        //如果任务列表中没有找到这个任务,那就去缓存任务列表找
        if (!exist)
        {
            for (int i = 0; i < tmpFrameList.Count; i++)
            {
                PETrameTask task = tmpFrameList[i];
                //任务列表中找到了 就在列表中删除这个任务
                if (task.tID == tID)
                {
                    tmpFrameList.RemoveAt(i);
                    //删除任务后,再从tid合集中把这个tid删除,让tid空闲出来,给其他的任务使用
                    for (int j = 0; j < tIDList.Count; j++)
                    {
                        if (tIDList[j] == tID)
                        {
                            tIDList.RemoveAt(j);
                            break;
                        }
                    }
                    exist = true;
                    break;
                }
            }
        }
        return(exist);
    }
Ejemplo n.º 3
0
    //检测帧任务是否应该执行
    public void CheckFrameTask()
    {
        //取出缓存区的任务,将里面的任务加入到即将执行的任务列表中
        for (int tmpIndex = 0; tmpIndex < tmpFrameList.Count; tmpIndex++)
        {
            taskFrameList.Add(tmpFrameList[tmpIndex]);
        }
        //缓存任务全部取出以后 清空缓存列表 以空出索引 好进行下次遍历
        tmpFrameList.Clear();

        //update每秒都会加帧
        frameCounter += 1;
        for (int index = 0; index < taskFrameList.Count; index++)
        {
            PETrameTask task = taskFrameList[index];
            //当前帧是否小于这个任务里面的帧
            if (frameCounter < task.destFrame)
            {
                //如果小于,说明时间还没到。就跳出,检测下一个任务
                continue;
            }
            else
            {
                //如果时间到了  就执行里面的任务(委托) 使用try捕获委托中可能出现的异常,防止因为委托的原因导致定时系统崩溃
                try
                {
                    task.callback?.Invoke(task.tID);
                }
                catch (Exception e)
                {
                    LogInfo(e.ToString());
                }


                //次数消耗完毕后移除任务
                if (task.count == 1)
                {
                    //执行完毕后 移除任务
                    taskFrameList.Remove(task);
                    //索引提前 继续遍历  如果觉得不妥 可以新建一个要执行的任务列表 进行循环执行
                    index--;
                    //回收tID
                    recList.Add(task.tID);
                }
                else
                {
                    if (task.count != 0)
                    {
                        task.count -= 1;
                    }
                    //下次执行的时间为目标时间加上延迟的时间
                    task.destFrame += task.delay;
                }
            }
            //如果有需要回收的ID 就执行回收函数 回收掉这些ID
            if (recList.Count > 0)
            {
                RecycleTid();
            }
        }
    }