コード例 #1
0
 /// <summary>
 /// 释放某个任务
 /// </summary>
 /// <param name="throttleTask"></param>
 public static void Release(IntermittentTask throttleTask)
 {
     lock (m_tasklock)
     {
         throttleTask.taskLite.Release();
     }
 }
コード例 #2
0
        /// <summary>
        /// 压入一个任务,按照一定时间间隔来执行
        /// </summary>
        /// <param name="task"></param>
        /// <param name="interval"></param>
        /// <param name="stopType"></param>
        /// <param name="stopValue"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static IntermittentTask Invoke(Action task, int interval, ThrottleStopType stopType, int stopValue, object tag = null)
        {
            lock (m_tasklock)
            {
                var throttleTask = new IntermittentTask
                {
                    Task      = task,
                    Interval  = interval,
                    stopType  = stopType,
                    stopValue = stopValue,
                    Tag       = tag
                };

                throttleTask.taskLite = TaskLite.Invoke(t =>
                {
                    if (t < throttleTask.lastRunTime + throttleTask.Interval)
                    {
                        return(false);
                    }

                    throttleTask.lastRunTime = t;

                    task?.Invoke();

                    throttleTask.Times++;

                    if (throttleTask.stopType == ThrottleStopType.Times)
                    {
                        return(throttleTask.Times >= stopValue);
                    }
                    else if (throttleTask.stopType == ThrottleStopType.Duration)
                    {
                        return(throttleTask.Timer >= stopValue);
                    }
                    else
                    {
                        return(false);
                    }
                });

                return(throttleTask);
            }
        }