Exemple #1
0
 /// <summary>Method to fire the event</summary>
 /// <param name="sender">Sender.</param>
 /// <param name="e">The event information</param>
 public void executeEvent(object sender, TimerManagerEventArg e)
 {
     if (eHandler != null)
     {
         eHandler(sender, e);
     }
 }
Exemple #2
0
        /// <summary>
        /// Method that does the ticking
        /// </summary>
        private static void tick()
        {        // tick!
            bool skip = false;

            while (true)
            {
                skip = false;

                if (isVerbose)
                {
                    LogManager.Instance.Log().Debug("Ticking!");
                }

                // if we're not running, don't tick
                if (!isRunning)
                {
                    if (isVerbose)
                    {
                        LogManager.Instance.Log().Debug("Timer not running while ticking. Abort.");
                    }
                    skip = true;
                }

                if (!skip)
                {
                    // increment our counter
                    tickCount++;

                    if (deferredCleanFlag)
                    {
                        LogManager.Instance.Log().Info("timer Event Cleared");
                        timerEvents       = new List <TimerManagerEventset>();
                        deferredCleanFlag = false;
                        //continue;
                    }

                    // make a new list that will replace the one once we're done with our tick
                    List <TimerManagerEventset> newList = new List <TimerManagerEventset>();

                    // copy the lists and wipe the old one
                    List <TimerManagerEventset> defAddListCopy = new List <TimerManagerEventset>(deferredAddList);
                    List <string> defRemListCopy = new List <string>(deferredRemList);
                    List <string> defPauListCopy = new List <string>(deferredPauseList);
                    List <string> defUnpListCopy = new List <string>(deferredUnpauseList);
                    deferredAddList     = new List <TimerManagerEventset>();
                    deferredRemList     = new List <string>();
                    deferredPauseList   = new List <string>();
                    deferredUnpauseList = new List <string>();

                    List <TimerManagerEventset> currentList = new List <TimerManagerEventset>(timerEvents);

                    // add the new stuff requested
                    if (defAddListCopy.Count > 0)
                    {
                        LogManager.Instance.Log().Debug("Adding " + defAddListCopy.Count.ToString() + " items");
                        currentList.AddRange(defAddListCopy);
                    }

                    // check each events
                    foreach (TimerManagerEventset t in currentList)
                    {
                        TimerManagerEventset newT = new TimerManagerEventset(t);

                        if (defRemListCopy.Contains(t.Id))
                        {
                            // we need to wipe that one.  don't even bother readding it to the new list
                            LogManager.Instance.Log().Debug("Removing " + t.Id);
                            continue;
                        }

                        if (defPauListCopy.Contains(t.Id))
                        {
                            LogManager.Instance.Log().Debug("Pausing " + t.Id);
                            newT.isPaused = true;
                            // we're not gonna fire since we just paused it
                            newList.Add(newT);
                            continue;
                        }
                        if (defUnpListCopy.Contains(t.Id))
                        {
                            // wake that one up!
                            LogManager.Instance.Log().Debug("Unpausing " + t.Id);
                            newT.isPaused = false;
                        }

                        if (DateTime.UtcNow >= newT.nextTick)
                        {// we're ready to tick
                            // if we're paused, don't even bother.  next!
                            if (t.isPaused)
                            {
                                if (isVerbose)
                                {
                                    LogManager.Instance.Log().Debug(string.Format("EventSet ID \"{0}\" is ready to tick but is paused.", newT.Id));
                                }
                                continue;
                            }
                            LogManager.Instance.Log().Debug(string.Format("EventSet ID \"{0}\" is ready to tick", newT.Id));
                            newT.nextTick = DateTime.UtcNow.AddMilliseconds(newT.timeInbetweenTick);
                            newList.Add(newT);
                            LogManager.Instance.Log().Debug(string.Format("Setting new tick time for EventSet ID \"{0}\" to {1}", newT.Id, newT.nextTick.ToLongTimeString()));

                            // fire
                            TimerManagerEventArg param = new TimerManagerEventArg();
                            param.tickTime = DateTime.UtcNow;
                            LogManager.Instance.Log().Debug("Firing event");
                            newT.executeEvent(new object(), param);
                        }
                        else
                        {
                            // nothing to do, just put back the data in the list
                            newList.Add(newT);
                        }
                    }

                    // swap the old list with the new updated one
                    timerEvents = newList;
                }

                // sleep
                if (isVerbose)
                {
                    LogManager.Instance.Log().Debug("Timer Sleeping.");
                }
                skip = false;

                // check if we need to tick next time
                isRunning = IsEventListFilled();

                Thread.Sleep(1000);
            }
        }