Example #1
0
    public bool ReplaceTimeTask(int tid, Action <int> callback, float delay, WinterTimeUnit timeUnit = WinterTimeUnit.Millisecond, int count = 1)
    {
        if (timeUnit != WinterTimeUnit.Millisecond)
        {
            switch (timeUnit)
            {
            case WinterTimeUnit.Second:
                delay = delay * 1000;
                break;

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

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

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

            default:
                LogInfo("Replace Task TimeUnit Type Error...");
                break;
            }
        }
        nowTime = GetUTCMilliseconds();
        WinterTimeTask newTask = new WinterTimeTask(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;
                    isRep         = true;
                    break;
                }
            }
        }

        return(isRep);
    }
Example #2
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++)
                    {
                        WinterTimeTask 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++)
                    {
                        WinterTimeTask task = tmpTimeLst[j];
                        if (task.tid == delTid)
                        {
                            tmpTimeLst.RemoveAt(j);
                            recTidLst.Add(delTid);
                            //LogInfo("Del tmpTimeLst ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
                            break;
                        }
                    }
                }
            }
        }
    }
Example #3
0
    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++)
        {
            WinterTimeTask 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;
                }
            }
        }
    }