コード例 #1
0
 internal static void DestroyTimer(TimerTask task)
 {
     lock (lck)
     {
         task.Dispose();
         timers.Remove(task);
     }
 }
コード例 #2
0
        private bool TryGetTimerFrom(TriggerReader reader, out TimerTask timerTask)
        {
            double num = reader.ReadNumber();

            if (num > 0)
            {
                timerTask = timers.FirstOrDefault(task => task.Id == num);
                return(timerTask != null);
            }
            timerTask = null;
            return(false);
        }
コード例 #3
0
 private static void timer_Elapsed(object sender)
 {
     try
     {
         TimerTask timerTask = (TimerTask)sender;
         if (timerTask.Timer.Enabled)
         {
             if (timerTask.FirstRun && timerTask.Delay > 0)
             {
                 Thread.Sleep(TimeSpan.FromSeconds(timerTask.Delay));
                 timerTask.FirstRun = false;
                 timerTask.Stop(); // fixes timer offset due to Delay bug
                 timerTask.Start();
             }
             Timers.CurrentTimer = timerTask.Id;
             timerTask.Owner.Execute(300, timerTask.Id);
             Timers.CurrentTimer = 0;
         }
     }
     catch
     {
         // Eat the exception.. yummy!
     }
 }
コード例 #4
0
        private bool CreateTimer(TriggerReader reader)
        {
            if (timers.Count >= timersLimit)
            {
                throw new MonkeyspeakException("The amount of timers has exceeded the limit of {0}", timersLimit);
            }
            double id = reader.ReadNumber();

            double interval = reader.ReadNumber();

            reader.TryReadNumber(out double delay);

            Logger.Debug <Timers>($"id={id} interval={TimeSpan.FromSeconds(interval)} delay = {delay}");
            if (interval <= 0)
            {
                return(false);
            }
            if (id <= 0)
            {
                return(false);         // NOTE no more timers with id 0, must be > 0
            }
            lock (lck)
            {
                var existing = timers.FirstOrDefault(task => task.Id == id);
                if (existing != null)
                {
#warning Replacing existing timer may cause any triggers dependent on that timer to behave differently
                    existing.Dispose();
                    timers.Remove(existing);
                }
                var timerTask = new TimerTask(reader.Page, interval, id, delay > 0 ? delay : 0);
                timers.Add(timerTask);
            }

            return(true);
        }