/// <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); } } }
/// <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); }
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; }
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); }
/// <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); }
/// <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); }
public static Int32 DecreasingComparison(WaitTimeAndAction x, WaitTimeAndAction y) { return((x.stopwatchTicksToHandleAction > y.stopwatchTicksToHandleAction) ? -1 : ((x.stopwatchTicksToHandleAction < y.stopwatchTicksToHandleAction) ? 1 : 0)); }