/// <summary>
 /// Checks if an action is scheduled.
 /// </summary>
 /// <param name="action">The action in question.</param>
 /// <returns>
 /// True if the action is scheduled, otherwise false.
 /// </returns>
 public bool isScheduled(NSFTimerAction action)
 {
     lock (threadMutex)
     {
         return(actions.Contains(action));
     }
 }
        /// <summary>
        /// Schedules an action with the timer.
        /// </summary>
        /// <param name="action">The action to schedule.</param>
        /// <param name="delayTime">The delay time before the action should execute.</param>
        /// <param name="repeatTime">The repeat time if the action is periodic, or 0 if the action is non-periodic.</param>
        public void scheduleAction(NSFTimerAction action, NSFTime delayTime, NSFTime repeatTime)
        {
            action.ExecutionTime = CurrentTime + delayTime;
            action.RepeatTime    = repeatTime;
            action.DelayTime     = delayTime;

            scheduleAction(action);
        }
 /// <summary>
 /// Executes the specified action, and reschedules it if a repeat time is specified.
 /// </summary>
 /// <param name="action">The action to execute.</param>
 private void executeAction(NSFTimerAction action)
 {
     // Guard a bad action from taking down timer thread
     try
     {
         action.execute();
     }
     catch (Exception exception)
     {
         handleException(new Exception(action.Name + " action execution exception", exception));
     }
 }
        /// <summary>
        /// Schedules an action to execute at its previously designated execution time.
        /// </summary>
        /// <param name="action">The action to schedule.</param>
        public void scheduleAction(NSFTimerAction action)
        {
            lock (threadMutex)
            {
                // Do not schedule any actions if terminating or terminated (i.e. not ready)
                if (TerminationStatus != NSFThreadTerminationStatus.ThreadReady)
                {
                    return;
                }

                insertAction(action);

                // Set next timeout if action was inserted in the front of the list
                if (action == actions.First.Value)
                {
                    timer.setNextTimeout(action.ExecutionTime);
                }
            }
        }
        private void insertAction(NSFTimerAction action)
        {
            // Make sure action is not already in list
            actions.Remove(action);

            // Insert into list based on execution time order
            // Actions with equal execution times are executed in FIFO order
            LinkedListNode <NSFTimerAction> nextNode = actions.First;

            while (nextNode != null)
            {
                if (action.ExecutionTime < nextNode.Value.ExecutionTime)
                {
                    actions.AddBefore(nextNode, action);
                    return;
                }
                nextNode = nextNode.Next;
            }

            // Insert action at end of list if not alread inserted
            actions.AddLast(action);
        }
        /// <summary>
        /// Unschedules a previously scheduled action.
        /// </summary>
        /// <param name="action">The action to unschedule.</param>
        /// <remarks>
        /// Unscheduling an action that is not currently scheduled has no effect.
        /// </remarks>
        public void unscheduleAction(NSFTimerAction action)
        {
            lock (threadMutex)
            {
                if (actions.Count != 0)
                {
                    if (action == actions.First.Value)
                    {
                        actions.Remove(action);

                        if (actions.Count != 0)
                        {
                            timer.setNextTimeout(actions.First.Value.ExecutionTime);
                        }
                    }
                    else
                    {
                        actions.Remove(action);
                    }
                }
            }
        }
        private void insertAction(NSFTimerAction action)
        {
            // Make sure action is not already in list
            actions.Remove(action);

            // Insert into list based on execution time order
            // Actions with equal execution times are executed in FIFO order
            LinkedListNode<NSFTimerAction> nextNode = actions.First;
            while (nextNode != null)
            {
                if (action.ExecutionTime < nextNode.Value.ExecutionTime)
                {
                    actions.AddBefore(nextNode, action);
                    return;
                }
                nextNode = nextNode.Next;
            }

            // Insert action at end of list if not alread inserted
            actions.AddLast(action);
        }
 /// <summary>
 /// Executes the specified action, and reschedules it if a repeat time is specified.
 /// </summary>
 /// <param name="action">The action to execute.</param>
 private void executeAction(NSFTimerAction action)
 {
     // Guard a bad action from taking down timer thread
     try
     {
         action.execute();
     }
     catch (Exception exception)
     {
         handleException(new Exception(action.Name + " action execution exception", exception));
     }
 }
        /// <summary>
        /// Unschedules a previously scheduled action.
        /// </summary>
        /// <param name="action">The action to unschedule.</param>
        /// <remarks>
        /// Unscheduling an action that is not currently scheduled has no effect.
        /// </remarks>
        public void unscheduleAction(NSFTimerAction action)
        {
            lock (threadMutex)
            {
                if (actions.Count != 0)
                {
                    if (action == actions.First.Value)
                    {
                        actions.Remove(action);

                        if (actions.Count != 0)
                        {
                            timer.setNextTimeout(actions.First.Value.ExecutionTime);
                        }
                    }
                    else
                    {
                        actions.Remove(action);
                    }
                }
            }
        }
        /// <summary>
        /// Schedules an action with the timer.
        /// </summary>
        /// <param name="action">The action to schedule.</param>
        /// <param name="delayTime">The delay time before the action should execute.</param>
        /// <param name="repeatTime">The repeat time if the action is periodic, or 0 if the action is non-periodic.</param>
        public void scheduleAction(NSFTimerAction action, NSFTime delayTime, NSFTime repeatTime)
        {
            action.ExecutionTime = CurrentTime + delayTime;
            action.RepeatTime = repeatTime;
            action.DelayTime = delayTime;

            scheduleAction(action);
        }
        /// <summary>
        /// Schedules an action to execute at its previously designated execution time.
        /// </summary>
        /// <param name="action">The action to schedule.</param>
        public void scheduleAction(NSFTimerAction action)
        {
            lock (threadMutex)
            {
                // Do not schedule any actions if terminating or terminated (i.e. not ready)
                if (TerminationStatus != NSFThreadTerminationStatus.ThreadReady)
                {
                    return;
                }

                insertAction(action);

                // Set next timeout if action was inserted in the front of the list
                if (action == actions.First.Value)
                {
                    timer.setNextTimeout(action.ExecutionTime);
                }
            }
        }
 /// <summary>
 /// Checks if an action is scheduled.
 /// </summary>
 /// <param name="action">The action in question.</param>
 /// <returns>
 /// True if the action is scheduled, otherwise false.
 /// </returns>
 public bool isScheduled(NSFTimerAction action)
 {
     lock (threadMutex)
     {
         return actions.Contains(action);
     }
 }