/// <summary> /// The do spin. /// </summary> /// <param name="loops"> /// The loops. /// </param> /// <param name="iterations"> /// The iterations. /// </param> /// <param name="notify"> /// The notify. /// </param> /// <param name="token"> /// The token. /// </param> public void DoSpin(int loops, int iterations, SpinNotify notify, CancellationToken token) { for (var i = 0; i < loops; i++) { WorkflowTrace.Verbose("TaskSpinWaiter loop {0} of {1}", i, loops); try { if (token.IsCancellationRequested) { WorkflowTrace.Verbose("TaskSpinWaiter Cancel Requested"); return; } Task.Run( () => { WorkflowTrace.Verbose("SpinWait({0})", iterations); Thread.SpinWait(iterations); if (token.IsCancellationRequested) { WorkflowTrace.Verbose("SpinWait - Cancellation is requested"); } }, token).Wait(); } finally { notify.LoopComplete(loops, iterations); } } WorkflowTrace.Verbose("TaskSpinWaiter done with {0} iterations", iterations * loops); }
/// <summary> /// Executes the spin wait /// </summary> /// <param name="token"> /// The token. /// </param> /// <param name="notify"> /// The notify. /// </param> private void DoSpinWait(ActivityCancellationToken token, SpinNotify notify) { for (var i = 0; i < this.Loops; i++) { WorkflowTrace.Verbose( "AsyncSpinWaiter loop {0} of {1}, spinning {2} iterations", i, this.Loops, this.Iterations); try { // For the token cancel if (token.IsCancellationRequested()) { WorkflowTrace.Verbose("Token requests cancel"); return; } Thread.SpinWait(this.Iterations); } finally { if (notify != null) { notify.LoopComplete(this.Loops, this.Iterations); } } } WorkflowTrace.Verbose("AsyncSpinWaiter done with {0} iterations", this.Iterations * this.Loops); }