Example #1
0
    /// <summary>
    /// 替换一个已经存在的倒计时任务
    /// </summary>
    /// <param name="tid">任务ID</param>
    /// <param name="callback">回调(int/string)</param>
    /// <param name="totalTime">任务总时间</param>
    /// <returns>替换结果</returns>
    public bool ReplaceCountdownTask(int tid, Action <int, string> callback, int totalTime)
    {
        bool result = false;


        PECountdownTask newTask = new PECountdownTask(tid, totalTime * 1000, callback);

        nowTime          = GetUTCMilliseconds();
        newTask.destTime = nowTime + newTask.delayTime;
        for (int i = 0; i < taskCountdownList.Count; i++)
        {
            if (newTask.tid == taskCountdownList[i].tid)
            {
                result = true;
                taskCountdownList[i] = newTask;
                break;
            }
        }

        if (!result)
        {
            for (int i = 0; i < tmpTaskCountdownList.Count; i++)
            {
                if (newTask.tid == tmpTaskCountdownList[i].tid)
                {
                    result = true;
                    tmpTaskCountdownList[i] = newTask;
                    break;
                }
            }
        }

        return(result);
    }
Example #2
0
    private void CheckCountdownTask()
    {
        //加入上一帧的缓存区的定时任务
        for (int tmpIndex = 0; tmpIndex < tmpTaskCountdownList.Count; tmpIndex++)
        {
            taskCountdownList.Add(tmpTaskCountdownList[tmpIndex]);
        }
        tmpTaskCountdownList.Clear();

        //遍历检测定时任务是否达到条件
        for (int i = 0; i < taskCountdownList.Count; i++)
        {
            PECountdownTask task = taskCountdownList[i];
            nowTime = GetUTCMilliseconds();
            if (nowTime < task.destTime)
            {
                continue;
            }
            else
            {
                Action <int>    cb     = task.intCallback;
                Action <string> cb_str = task.strCallback;
                try
                {
                    if (cb != null)
                    {
                        cb(task.totalTime / 1000);
                    }
                    if (cb_str != null)
                    {
                        cb_str(GetDateTime(task.totalTime / 1000));
                    }
                }
                catch (Exception e)
                {
                    Log(e.ToString());
                }

                //移除已经完成的定时任务
                if (task.totalTime <= 0)
                {
                    recTidList.Add(task.tid);
                    taskCountdownList.RemoveAt(i);
                    i--;
                }
                else
                {
                    if (task.totalTime > 0)
                    {
                        task.totalTime -= 1000;
                    }
                    task.destTime += task.delayTime;
                }
            }
        }
    }
Example #3
0
    /// <summary>
    /// 添加一个倒计时任务
    /// </summary>
    /// <param name="callback">回调</param>
    /// <param name="totalTime">倒计时总时间</param>
    /// <returns>任务ID</returns>
    public int AddCountdownTask(Action <int, string> callback, int totalTime)
    {
        int tid = GetTid();

        tidList.Add(tid);

        PECountdownTask tmpTask = new PECountdownTask(tid, totalTime * 1000, callback);

        nowTime          = GetUTCMilliseconds();
        tmpTask.destTime = nowTime + tmpTask.delayTime;
        tmpTaskCountdownList.Add(tmpTask);
        Log("添加了一个倒计时任务,Tid = " + tid);
        return(tid);
    }