Exemplo n.º 1
0
        public bool ReplaceTimeTask(int tid, Action <int> callback, float delay, ILTimerUnit timerUnit = ILTimerUnit.Millisecond, int count = 1)
        {
            if (timerUnit != ILTimerUnit.Millisecond)
            {
                switch (timerUnit)
                {
                case ILTimerUnit.Second:
                    delay = delay * 1000;
                    break;

                case ILTimerUnit.Mintue:
                    delay = delay * 1000 * 60;
                    break;

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

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

                default:
                    LogInfo("Replace Task TimeUnit Type Error...");
                    break;
                }
            }

            nowTime = GetUTCMilliseconds();
            ILTimerTask newTask = new ILTimerTask(tid, callback, nowTime + delay, delay, count);

            bool isRep = false;

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

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

            return(isRep);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 删除时间任务
        /// </summary>
        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++)
                        {
                            ILTimerTask task = taskTimeLst[j];
                            if (task.tid == delTid)
                            {
                                isDel = true;
                                taskTimeLst.RemoveAt(j);

                                break;
                            }
                        }

                        if (isDel)
                        {
                            continue;
                        }

                        for (int j = 0; j < tmpTimeLst.Count; j++)
                        {
                            ILTimerTask task = tmpTimeLst[j];
                            if (task.tid == delTid)
                            {
                                tmpTimeLst.RemoveAt(j);
                                recTidLst.Add(delTid);

                                break;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 检查当前时间任务
        /// </summary>
        private void CheckTimeTask()
        {
            if (tmpTimeLst.Count > 0)
            {
                lock (lockTime)
                {
                    //加入缓存区中的定时任务
                    for (int tmpIndex = 0; tmpIndex < tmpTimeLst.Count; tmpIndex++)
                    {
                        taskTimeLst.Add(tmpTimeLst[tmpIndex]);
                    }

                    tmpTimeLst.Clear();
                }
            }

            //遍历检测任务是否达到条件
            nowTime = GetMillisecondsTime();
            for (int index = 0; index < taskTimeLst.Count; index++)
            {
                ILTimerTask 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;
                    }
                }
            }
        }