Esempio n. 1
0
        /// <summary>
        /// Handle wait actions
        /// </summary>
        /// <returns>Return milliseconds from now of the next action, or 0 if there are no more actions</returns>
        public Int32 HandleWaitActions()
        {
            while (true)
            {
                if (waitActions.count <= 0)
                {
                    return(0);
                }

                //
                // Get the soonest action and check if it should be executed
                //
                WaitTimeAndAction nextWaitTimeAndAction = waitActions.elements[waitActions.count - 1];

                Int32 millisFromNow = nextWaitTimeAndAction.MillisFromNow();
                if (millisFromNow > millisecondTolerance)
                {
                    return(millisFromNow);
                }

                //
                // Remove the soonest action from the list
                //
                waitActions.count--;
                waitActions.elements[waitActions.count] = null; // remove reference

                //
                // Execute the action, and add it back to the list if it was reset
                //
                if (nextWaitTimeAndAction.Execute())
                {
                    waitActions.Add(nextWaitTimeAndAction);
                }
            }
        }
Esempio n. 2
0
        public UInt32 WaitActionTimes(ArrayReference <Int32> times, Int64 now)
        {
            times.EnsureCapacityCopyData(waitActions.count);

            UInt32 count = 0;

            for (int i = (Int32)waitActions.count - 1; i >= 0; i--)
            {
                WaitTimeAndAction nextWaitTimeAndAction = waitActions.elements[i];
                times.array[count] = nextWaitTimeAndAction.MillisFromNow(now);
                count++;
            }

            return(count);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds a new wait time and action, if the new time is sooner than any previous times it returns true.
        /// This can be used to signal to another thread that it needs to reset it's timer till the next event.
        /// </summary>
        /// <param name="newWaitTimeAndAction">The new wait time and action to add</param>
        public Boolean Add(WaitTimeAndAction newWaitTimeAndAction)
        {
            if (!newWaitTimeAndAction.HasAction())
            {
                throw new InvalidOperationException("Cannot add this wait time action because it has no action");
            }

            if (waitActions.count <= 0)
            {
                waitActions.Add(newWaitTimeAndAction);
                return(true);
            }

            //
            // Check if this new event happens sooner than the next event
            //
            Int64 now = Stopwatch.GetTimestamp();
            Int32 nextEventMillisFromNow = waitActions.elements[waitActions.count - 1].MillisFromNow(now);
            Int32 newEventMillisFromNow  = newWaitTimeAndAction.MillisFromNow(now);

            waitActions.Add(newWaitTimeAndAction);

            return(newEventMillisFromNow < nextEventMillisFromNow);
        }