예제 #1
0
    private void DelTimeTask()
    {
        if (tmpDelTimeLst.Count > 0)
        {
            lock (lockTime)
            {
                for (int i = 0; i < tmpDelTimeLst.Count; i++)
                {
                    bool isDel  = false;
                    int  delTid = tmpDelTimeLst[i];
                    for (int j = 0; j < taskTimeLst.Count; j++)
                    {
                        PETimeTask task = taskTimeLst[j];
                        if (task.tid == delTid)
                        {
                            isDel = true;
                            taskTimeLst.RemoveAt(j);
                            recTidLst.Add(delTid);
                            //LogInfo("Del taskTimeLst ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
                            break;
                        }
                    }

                    if (isDel)
                    {
                        continue;
                    }

                    for (int j = 0; j < tmpTimeLst.Count; j++)
                    {
                        PETimeTask task = tmpTimeLst[j];
                        if (task.tid == delTid)
                        {
                            tmpTimeLst.RemoveAt(j);
                            recTidLst.Add(delTid);
                            //LogInfo("Del tmpTimeLst ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
                            break;
                        }
                    }
                }
            }
        }
    }
예제 #2
0
 /// <summary>
 /// 给定id和PETimeTask列表,查询该PETimeTask列表中PETimeTask是否有该id,并删除
 /// </summary>
 /// <param name="id"></param>
 /// <param name="TimeList"></param>
 /// <returns></returns>
 private bool SearchIdExistAndDelete(int id, List <PETimeTask> TimeList)
 {
     for (int i = 0; i < TimeList.Count; i++)
     {
         PETimeTask timeTask = TimeList[i];
         if (timeTask.id == id)
         {
             TimeList.RemoveAt(i);
             for (int j = 0; j < idList.Count; j++)
             {
                 if (id == idList[j])
                 {
                     idList.RemoveAt(j);
                     break;;
                 }
             }
             return(true);
         }
     }
     return(false);
 }
예제 #3
0
    /// <summary>
    /// 添加一个时间定时任务
    /// </summary>
    /// <param name="callback">回调</param>
    /// <param name="delayTime">延时时间</param>
    /// <param name="unit">时间单位(默认毫秒)</param>
    /// <param name="count">执行次数(0循环)</param>
    /// <returns>任务ID</returns>
    public int AddTimeTask(Action callback, float delayTime, PETimeUnit unit = PETimeUnit.Millsecond, int count = 1)
    {
        //时间换算为毫秒
        if (unit != PETimeUnit.Millsecond)
        {
            switch (unit)
            {
            case PETimeUnit.Second:
                delayTime = delayTime * 1000;
                break;

            case PETimeUnit.Minute:
                delayTime = delayTime * 1000 * 60;
                break;

            case PETimeUnit.Hour:
                delayTime = delayTime * 1000 * 60 * 60;
                break;

            case PETimeUnit.Day:
                delayTime = delayTime * 1000 * 60 * 60 * 24;
                break;

            default:
                break;
            }
        }

        int tid = GetTid();

        tidList.Add(tid);

        // Time.realtimeSinceStartup 从游戏启动到现在为止的时间
        nowTime = GetUTCMilliseconds() + delayTime;
        PETimeTask tmpTask = new PETimeTask(tid, nowTime, delayTime, count, callback);

        tmpTimeList.Add(tmpTask);

        return(tid);
    }
예제 #4
0
    private void DelTimeTask()
    {
        if (tmpDelTimeLst.Count > 0)
        {
            lock (lockTmpDelTimeLst) {
                for (int i = 0; i < tmpDelTimeLst.Count; i++)
                {
                    int tid = tmpDelTimeLst[i];
                    for (int j = 0; j < taskTimeLst.Count; j++)
                    {
                        PETimeTask task = taskTimeLst[j];
                        if (task.tid == tid)
                        {
                            taskTimeLst.RemoveAt(j);
                            Console.WriteLine("Del1线程ID:{0}", Thread.CurrentThread.ManagedThreadId.ToString());
                            recTidLst.Add(tid);
                            break;
                        }
                    }
                }

                for (int i = 0; i < tmpDelTimeLst.Count; i++)
                {
                    int tid = tmpDelTimeLst[i];
                    for (int j = 0; j < tmpTimeLst.Count; j++)
                    {
                        PETimeTask task = tmpTimeLst[j];
                        if (task.tid == tid)
                        {
                            tmpTimeLst.RemoveAt(j);
                            Console.WriteLine("Del2线程ID:{0}", Thread.CurrentThread.ManagedThreadId.ToString());
                            recTidLst.Add(tid);
                            break;
                        }
                    }
                }
            }
        }
    }
예제 #5
0
    public void AddTimeTask(Action callBack, float delayTime, PetimeUnit timeUnit = PetimeUnit.Millsecond)
    {
        float time = delayTime;

        if (timeUnit != PetimeUnit.Millsecond)
        {
            switch (timeUnit)
            {
            case PetimeUnit.Second:
                time = time * 1000;
                break;

            case PetimeUnit.Minute:
                time = time * 60 * 1000;
                break;

            case PetimeUnit.Hour:
                time = time * 60 * 60 * 1000;
                break;

            case PetimeUnit.day:
                time = time * 24 * 60 * 60 * 1000;
                break;

            default:
                Debug.Log("Add Task TimeUnit Type Error......");
                break;
            }
        }
        float      destTime = Time.realtimeSinceStartup + time;
        PETimeTask timeTask = new PETimeTask
        {
            destTime = destTime,
            callBack = callBack
        };

        tempTimeList.Add(timeTask);
    }
예제 #6
0
파일: PETimer.cs 프로젝트: wangzeping1998/-
    private void CheckTimeTask()
    {
        if (tmpTimeLst.Count > 0)
        {
            lock (lockTime) {
                //加入缓存区中的定时任务
                for (int tmpIndex = 0; tmpIndex < tmpTimeLst.Count; tmpIndex++)
                {
                    taskTimeLst.Add(tmpTimeLst[tmpIndex]);
                }
                tmpTimeLst.Clear();
            }
        }

        //遍历检测任务是否达到条件
        nowTime = GetUTCMilliseconds();
        for (int index = 0; index < taskTimeLst.Count; index++)
        {
            PETimeTask task = taskTimeLst[index];
            if (nowTime.CompareTo(task.destTime) < 0)
            {
                continue;
            }
            else
            {
                Action <int> cb = task.callback;
                try {
                    if (taskHandle != null)
                    {
                        taskHandle(cb, task.tid);
                    }
                    else
                    {
                        if (cb != null)
                        {
                            cb(task.tid);
                        }
                    }
                }
                catch (Exception e) {
                    LogInfo(e.ToString());
                }

                //移除已经完成的任务
                if (task.count == 1)
                {
                    taskTimeLst.RemoveAt(index);
                    index--;
                    recTidLst.Add(task.tid);
                }
                else
                {
                    if (task.count != 0)
                    {
                        task.count -= 1;
                    }
                    task.destTime += task.delay;
                }
            }
        }
    }
예제 #7
0
    //检测定时任务是否应该执行
    public void CheckTimeTask()
    {
        //如果临时列表里还有未取出的任务,就上锁,防止添加任务的时候被清理

        nowTime = GetUTCSeconds();
        //取出缓存区的任务,将里面的任务加入到即将执行的任务列表中
        for (int tmpIndex = 0; tmpIndex < tmpTimeList.Count; tmpIndex++)
        {
            taskTimeList.Add(tmpTimeList[tmpIndex]);
        }
        //缓存任务全部取出以后 清空缓存列表 以空出索引 好进行下次遍历
        tmpTimeList.Clear();

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


                //次数消耗完毕后移除任务
                if (task.count == 1)
                {
                    //执行完毕后 移除任务
                    taskTimeList.Remove(task);
                    //索引提前 继续遍历  如果觉得不妥 可以新建一个要执行的任务列表 进行循环执行
                    index--;
                    //回收tID
                    recList.Add(task.tID);
                }
                else
                {
                    if (task.count != 0)
                    {
                        task.count -= 1;
                    }
                    //下次执行的时间为目标时间加上延迟的时间
                    task.destTime += task.delay;
                }
            }
            //如果有需要回收的ID 就执行回收函数 回收掉这些ID
            if (recList.Count > 0)
            {
                RecycleTid();
            }
        }
    }
예제 #8
0
 /// <summary>
 /// 时间计时器主运行方法
 /// </summary>
 private void BeginTiming()
 {
     for (int tempIndex = 0; tempIndex < tempTimeList.Count; tempIndex++)//遍历缓存列表,并加入至操作列表
     {
         taskTimeList.Add(tempTimeList[tempIndex]);
     }
     tempTimeList.Clear();                                           //清除缓存列表
     for (int i = 0; i < taskTimeList.Count; i++)                    //遍历操作列表进行操作判定
     {
         PETimeTask timeTask = taskTimeList[i];                      //临时赋值
         if (timeTask.updateModel)                                   //Update形式
         {
             if (GetTime() - timeTask.destTime < 0)                  //没有达到目标时间.进行下一任务判断
             {
                 Action <double> callback = timeTask.updateCallback; //方法赋值
                 try
                 {
                     callback?.Invoke(timeTask.destTime - GetTime());//执行方法
                 }
                 catch (Exception e)
                 {
                     Console.WriteLine(e);
                     throw;
                 }
             }
             else
             {
                 Action callback = timeTask.callback;//方法赋值
                 try
                 {
                     callback?.Invoke();//执行方法
                 }
                 catch (Exception e)
                 {
                     Console.WriteLine(e);
                     throw;
                 }
                 if (timeTask.count == 1)
                 {
                     taskTimeList.RemoveAt(i); //移除当前任务
                     i--;                      //转为下一任务
                     recycleList.Add(timeTask.id);
                 }
                 else if (timeTask.count != 0)
                 {
                     timeTask.count--;
                     timeTask.destTime += timeTask.delayTime;
                 }
                 else
                 {
                     timeTask.destTime += timeTask.delayTime;
                 }
             }
         }
         else
         {
             if (GetTime() - timeTask.destTime < 0)//没有达到目标时间.进行下一任务判断
             {
                 continue;
             }
             else//达到当前时间
             {
                 Action callback = timeTask.callback;//方法赋值
                 try
                 {
                     callback?.Invoke();//执行方法
                 }
                 catch (Exception e)
                 {
                     Console.WriteLine(e);
                     throw;
                 }
                 if (timeTask.count == 1)
                 {
                     taskTimeList.RemoveAt(i); //移除当前任务
                     i--;                      //转为下一任务
                     recycleList.Add(timeTask.id);
                 }
                 else if (timeTask.count != 0)
                 {
                     timeTask.count--;
                     timeTask.destTime += timeTask.delayTime;
                 }
                 else
                 {
                     timeTask.destTime += timeTask.delayTime;
                 }
             }
         }
     }
     RecycleId();
 }
예제 #9
0
파일: PETimer.cs 프로젝트: mengtest/PETimer
    void CheckTimeTask()
    {
        //增加临时列表 因为服务器循环比update快 为了数据安全
        if (tmpTimes.Count > 0)
        {
            lock (lockTime)
            {
                //加入缓存区中的定时任务
                for (int i = 0; i < tmpTimes.Count; i++)
                {
                    taskTimes.Add(tmpTimes[i]);
                }
                tmpTimes.Clear();
            }
        }

        nowTime = GetUTCMilliseconds();
        //遍历检测任务是否到达条件
        for (int i = 0; i < taskTimes.Count; i++)
        {
            PETimeTask task = taskTimes[i];
            //nowTime>task.destTime 1 nowTime<task.destTime -1 nowTime=task.destTime 0
            if (nowTime.CompareTo(task.destTime) < 0)
            {
                continue;
            }
            else
            {
                try
                {
                    if (taskHandle != null)
                    {
                        taskHandle(task.callBack, task.tid);
                    }
                    else
                    {
                        //时间到 callBack不为空调用
                        task.callBack?.Invoke(task.tid);
                    }
                }
                catch (Exception e)
                {
                    LogInfo(e.ToString());
                }

                if (task.count == 1)
                {
                    taskTimes.RemoveAt(i);
                    i--;
                    recTids.Add(task.tid);
                }
                else
                {
                    if (task.count != 0)
                    {
                        task.count -= 1;
                    }
                    //重新赋值时间
                    task.destTime += task.delay;
                }
            }
        }
    }