/// <summary> /// Method to Add EventSets /// </summary> /// <returns><c>true</c>, if EventSet was added, <c>false</c> otherwise.</returns> /// <param name="ID">the Identifier to associate this EventSet to</param> /// <param name="ms">the number of milliseconds inbetween ticks</param> /// <param name="handler">The event handler to call when ticking</param> /// <param name="tickNow">If set to <c>true</c> tick as soon as possible</param> public static bool AddEventSet(string ID, int ms, TimerManagerEventHandler handler, bool tickNow) { LogManager.Instance.Log().Debug(string.Format("Adding EventSet with parameters: ID: {0}, ms: {1}, handler: {2}, tickNow: {3}", ID, ms.ToString(), handler.Method, tickNow.ToString())); if (IsIdentExist(ID)) { LogManager.Instance.Log().Debug("ID already existed. abort."); return(false); } TimerManagerEventset e = new TimerManagerEventset(); e.eHandler += handler; e.timeInbetweenTick = ms; e.Id = ID; if (tickNow) { // fire at the next timer itteration e.nextTick = DateTime.UtcNow; } else { // fire after X seconds e.nextTick = DateTime.UtcNow.AddMilliseconds(ms); } LogManager.Instance.Log().Debug(string.Format("Next Tick at: {0}", e.nextTick.ToLongTimeString())); deferredAddList.Add(e); // automatically restart the timer since we added something isRunning = true; return(true); }
/// <summary> /// Constuctor for an eventset based on a previous one. /// </summary> /// <param name="t">T.</param> public TimerManagerEventset(TimerManagerEventset t) { Id = t.Id; nextTick = t.nextTick; timeInbetweenTick = t.timeInbetweenTick; isPaused = t.isPaused; eHandler = t.eHandler; }
/// <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); } }