Ejemplo 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);
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Adds a new wait time and action.
 /// </summary>
 /// <param name="newWaitTimeAndAction">The new wait time and action to add</param>
 public void Add(WaitTimeAndAction newWaitTimeAndAction)
 {
     if (!newWaitTimeAndAction.HasAction())
     {
         throw new InvalidOperationException("Cannot add this wait time action because it has no action");
     }
     waitActions.Add(newWaitTimeAndAction);
 }
Ejemplo n.º 3
0
 public void HandleEvent(WaitTimeAndAction waitTimeAndAction)
 {
     if (this.expectedToHandle == false)
     {
         Assert.Fail("Did not expect to handle this event yet");
     }
     Console.WriteLine("Handling {0}", id);
     this.expectedToHandle = false;
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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 AddAndReturnTrueIfNewer(WaitTimeAndAction newWaitTimeAndAction)
        {
            if (!newWaitTimeAndAction.HasAction())
            {
                throw new InvalidOperationException("Cannot add this wait time action because it has no action");
            }
            Boolean isNewer = true;

            if (waitActions.count > 0)
            {
                isNewer = WaitTimeAndAction.DecreasingComparison(
                    newWaitTimeAndAction, waitActions.elements[waitActions.count - 1]) > 0;
            }
            waitActions.Add(newWaitTimeAndAction);
            return(isNewer);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
 public static Int32 DecreasingComparison(WaitTimeAndAction x, WaitTimeAndAction y)
 {
     return((x.stopwatchTicksToHandleAction > y.stopwatchTicksToHandleAction) ? -1 :
            ((x.stopwatchTicksToHandleAction < y.stopwatchTicksToHandleAction) ? 1 : 0));
 }