public void Run(ITimeout timeout)
 {
     channel.ExecuteInIoThread(new DelegateRunnable(() =>
     {
         try
         {
             timerTask.Run(timeout);
         }
         catch (Exception e)
         {
             channel.NettyChannel.Pipeline.FireExceptionCaught(e);
         }
     }));
 }
Example #2
0
        /// <summary>
        /// 检查定时器
        /// </summary>
        private void CheckTimer()
        {
            //计算下次执行时间
            DateTime?NextRunTime = getNextRunTime();

            //如果无法获得执行时间则 不再执行
            if (NextRunTime.HasValue)
            {
                while (true)
                {
                    DateTime DateTimeNow = DateTime.Now;

                    //时间比较
                    bool dateComp = DateTimeNow.Year == NextRunTime.Value.Year && DateTimeNow.Month == NextRunTime.Value.Month && DateTimeNow.Day == NextRunTime.Value.Day;

                    bool timeComp = DateTimeNow.Hour == NextRunTime.Value.Hour && DateTimeNow.Minute == NextRunTime.Value.Minute && DateTimeNow.Second == NextRunTime.Value.Second;

                    //睡够一分钟 防止一分钟内重复执行
                    Thread.Sleep(60 * 1000);
                    //如果当前时间等式下次运行时间,则调用线程执行方法
                    if (dateComp && timeComp)
                    {
                        //调用执行处理方法
                        if (TimerTaskDelegateFun != null)
                        {
                            TimerTaskDelegateFun(parm);
                        }
                        else if (TimerTaskInstance != null)
                        {
                            TimerTaskInstance.Run();
                        }
                        //重新计算下次执行时间
                        NextRunTime = getNextRunTime();
                    }
                }
            }
        }
Example #3
0
            public void Expire()
            {
                if (!CompareAndSetState(ST_IN_BUCKET, ST_EXPIRED))
                {
                    //assert this.state != ST_INIT
                    return;
                }

                try
                {
                    _task.Run(this);
                }
                catch (Exception t)
                {
                    _timer.Logger.WriteWarning($"{typeof(ITimerTask).Name} 执行出错。", t);
                    t.ThrowIfNecessary();
                }
            }