/// <summary> /// Adds a task to the scheduler /// </summary> /// <param name="_delegate">Callback to execute on matching time</param> /// <param name="_state">State object to pass to the Callback</param> /// <param name="_execute">Timespan when to execute the Callback</param> /// <param name="_interval">Interval when to re-execute Callback, if any</param> /// <param name="_continuous">Should this Task be continuously rescheduled</param> public void AddTask(Delegate _delegate, object _state, TimeSpan _execute, TimeSpan _interval, bool _continuous) { lock (m_Locker) { PluginTask task = new PluginTask(_delegate, _state, _continuous, _interval); DateTime now = DateTime.Now; now += _execute; DictionaryEntry entry = new DictionaryEntry(now.ToString(m_timerFormat), task); m_Tasks.Add(entry); } }
/// <summary> /// Main polling thread /// </summary> /// <param name="_state">Any required state information</param> private void PollTasks(object _state) { DateTime now = DateTime.Now; string s_now = now.ToString(m_timerFormat); //Debug.Print("Polling plugin list at: " + now.ToString(m_timerFormat)); foreach (DictionaryEntry item in m_Tasks) { //Debug.Print(item.Key.ToString()); if (item.Key.Equals(s_now)) { Debug.Print("Executing task"); // Execute task in Value PluginTask task = (PluginTask)item.Value; PluginEventHandler callback = (PluginEventHandler)task.CallBack; if (callback != null) { callback(task.State); } if (task.Reschedule) { now += task.Interval; item.Key = now.ToString(m_timerFormat); Debug.Print("Rescheduling task for " + now.ToString()); } else { lock (m_Locker) { m_Tasks.Remove(item); } } } } }