TimerRegister.cs

src/Updates/TimerRegister.cs · 1.6 KB

From the archive Arthea-src-1.0.zip

Download the archive   « Back to the archive contents

#region Arthea License

/***********************************************************************
*  Arthea MUD by R. Jennings (2007)      http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses. *
***********************************************************************/

#endregion

// Taken from Suroden (http://sourceforge.net/projects/suroden)
// - RJ, Aug'07

using System;

namespace Arthea.Updates
{
    /// <summary>
    /// Summary description for TimerRegister.
    /// </summary>
    public class TimerRegistry
    {
        #region [rgn] Fields (1)

        private readonly BinaryPriorityQueue<TimerAction> queue = new BinaryPriorityQueue<TimerAction>();

        #endregion [rgn]

        #region [rgn] Methods (2)

        // [rgn] Public Methods (2)

        /// <summary>
        /// Adds the timer action.
        /// </summary>
        /// <param name="a">A.</param>
        public void AddTimerAction(TimerAction a)
        {
            queue.Push(a);
        }

        /// <summary>
        /// Runs this instance.
        /// </summary>
        public void Run()
        {
            double now = DateTime.Now.TimeOfDay.TotalMilliseconds;

            while (queue.Count > 0 && queue.Peek().time <= now)
            {
                TimerAction a = queue.Pop();

                a.Execute();

                if (a.interval > 0)
                {
                    a.time = now + a.interval;
                    queue.Push(a);
                }
            }
        }

        #endregion [rgn]
    }
}

Site Stats

Snippets: 1791
Codebases: 571