Esempio n. 1
0
        /// <summary>
        /// 添加定时的任务
        /// </summary>
        /// <param name="callback">任务回调</param>
        /// <param name="delay">延迟时间</param>
        /// <param name="loopCount">循环次数(0 为 无限循环)</param>
        /// <param name="timerUnit">时间单位(ms,s,m,h,Day)</param>
        /// <returns>返回任务id</returns>
        public int AddTimerTask(Action callback, float delay, int loopCount = 1, TimerUnit timerUnit = TimerUnit.Millisecond)
        {
            if (timerUnit != TimerUnit.Millisecond)
            {
                switch (timerUnit)
                {
                case TimerUnit.Second:
                    delay = delay * 1000;
                    break;

                case TimerUnit.Minute:
                    delay = delay * 1000 * 60;
                    break;

                case TimerUnit.Hour:
                    delay = delay * 1000 * 60 * 60;
                    break;

                case TimerUnit.Day:
                    delay = delay * 1000 * 60 * 60 * 24;
                    break;

                default:
                    Debug.Log("Add Tssk Unit Type Error...");

                    break;
                }
            }
            int tid = GetTaskId();

            float     destTime  = GetCurrentTimeTick() + delay;
            TimerTask timerTask = new TimerTask(tid, callback, destTime, delay, loopCount);

            // 添加新定时任务的时候,先添加到临时列表(避免同帧遍历任务列表时干扰遍历列表)
            tmpTaskTimeList.Add(timerTask);

            // tid 添加到使用列表中
            taskIdList.Add(tid);

            return(tid);
        }
Esempio n. 2
0
    private void CalculateDelay(ref float delay, TimerUnit timerUnit)
    {
        //转换时间单位
        switch (timerUnit)
        {
        case TimerUnit.MilliSecond:
            delay *= 0.001f;
            break;

        case TimerUnit.Minute:
            delay *= 60;
            break;

        case TimerUnit.Hour:
            delay *= 60 * 60;
            break;

        case TimerUnit.Day:
            delay *= 60 * 60 * 24;
            break;
        }
    }
Esempio n. 3
0
 public override string GetFormula()
 {
     return("Timer unit=" + TimerUnit.ToString());
 }
Esempio n. 4
0
        /// <summary>
        /// 替换存在的任务
        /// </summary>
        /// <param name="taskId">任务id</param>
        /// <param name="callback">任务回调</param>
        /// <param name="delay">延迟时间</param>
        /// <param name="loopCount">循环次数(0 为 无限循环)</param>
        /// <param name="timerUnit">时间单位(ms,s,m,h,Day)</param>
        /// <returns>返回是否替换成功</returns>
        public bool ReplaceTimerTask(int taskId, Action callback, float delay, int loopCount = 1, TimerUnit timerUnit = TimerUnit.Millisecond)
        {
            if (timerUnit != TimerUnit.Millisecond)
            {
                switch (timerUnit)
                {
                case TimerUnit.Second:
                    delay = delay * 1000;
                    break;

                case TimerUnit.Minute:
                    delay = delay * 1000 * 60;
                    break;

                case TimerUnit.Hour:
                    delay = delay * 1000 * 60 * 60;
                    break;

                case TimerUnit.Day:
                    delay = delay * 1000 * 60 * 60 * 24;
                    break;

                default:
                    Debug.Log("Add Tssk Unit Type Error...");

                    break;
                }
            }


            float     destTime     = GetCurrentTimeTick() + delay;
            TimerTask newTimerTask = new TimerTask(taskId, callback, destTime, delay, loopCount);

            bool isReplace = false;

            // 从任务列表中查找对应的 taskId 进行任务替换
            for (int i = 0; i < taskTimeList.Count; i++)
            {
                TimerTask task = taskTimeList[i];

                if (task.taskId == taskId)
                {
                    taskTimeList[i] = newTimerTask;

                    isReplace = true;
                    break;
                }
            }

            // 从临时缓冲任务列表中替换
            if (isReplace == false)
            {
                for (int i = 0; i < tmpTaskTimeList.Count; i++)
                {
                    TimerTask task = tmpTaskTimeList[i];
                    if (task.taskId == taskId)
                    {
                        tmpTaskTimeList[i] = newTimerTask;
                        isReplace          = true;
                        break;
                    }
                }
            }


            return(isReplace);
        }
Esempio n. 5
0
 /// <summary>
 /// 时间类构造函数
 /// </summary>
 /// <param name="path">时间文件路径</param>
 public Time(string path = null)
 {
     TimeUnit = new TimerUnit(path);
 }
Esempio n. 6
0
    /// <summary>
    /// 替换时间定时任务
    /// </summary>
    /// <param name="id"></param>
    /// <param name="taskCall"></param>
    /// <param name="delay"></param>
    /// <param name="executeCount"></param>
    /// <param name="timerUnit"></param>
    /// <returns></returns>
    public bool ReplaceTimeTask(int id, Action <int> taskCall, float delay, int executeCount = 1, bool isExeInMain = false, Action <Action <int>, int> exeInMain = null, TimerUnit timerUnit = TimerUnit.Second)
    {
        CalculateDelay(ref delay, timerUnit);

        //生成taskModel
        double desTime = GetUTCSecond() + delay;

        foreach (var task in timeTaskList)
        {
            if (task.id == id)
            {
                //找到了
                task.Set(desTime, delay, executeCount, taskCall, isExeInMain, exeInMain);
                return(true);
            }
        }

        foreach (var task in tempTimeTaskList)
        {
            if (task.id == id)
            {
                //找到了
                task.Set(desTime, delay, executeCount, taskCall, isExeInMain, exeInMain);
                return(true);
            }
        }

        return(false);
    }
Esempio n. 7
0
    /// <summary>
    /// 添加时间定时任务
    /// </summary>
    /// <param name="taskCall">回调方法</param>
    /// <param name="delay">延迟多久执行</param>
    /// <param name="executeCount">执行次数</param>
    /// <param name="isExeInMain">是否在主线程中执行</param>
    /// <param name="timerUnit">设置delay参数的时间单位</param>
    /// <returns>生成的任务id</returns>
    public int AddTimeTask(Action <int> taskCall, float delay, int executeCount = 1, bool isExeInMain = false, Action <Action <int>, int> exeInMain = null, TimerUnit timerUnit = TimerUnit.Second)
    {
        CalculateDelay(ref delay, timerUnit);

        //生成taskModel
        double desTime = GetUTCSecond() + delay;
        int    taskId;

        //防止id重复
        while (true)
        {
            taskId = id.Add_Get();
            if (!taskIdList.Contains(taskId))
            {
                break;
            }
        }
        lock (lockTime)
        {
            tempTimeTaskList.Add(new TaskTimeModel(taskId, desTime, delay, executeCount, taskCall, isExeInMain, exeInMain));
        }
        lock (lockId)
        {
            taskIdList.Add(taskId);
        }

        return(taskId);
    }