/// <summary> /// Asynchronous evaluation of an <see cref="Assert"/> /// </summary> /// <remarks> /// This method is used as the <see cref="WaitOrTimerCallback"/> delegate for the /// <see cref="ThreadPool.RegisterWaitForSingleObject"/> version of the asynchonous call. /// <para> /// This just retrieves the state, casts it to <see cref="AsyncAssertState"/> and executes the /// <see cref="EvaluateAssert"/> method, passing the values received. It locks on the received /// <see cref="StringBuilder"/> to append the messages from evaluation. /// </para> /// </remarks> /// <param name="state">State for the execution.</param> /// <param name="timedOut">If the <see cref="WaitHandle"/> timed out.</param> private void OnAssertEvaluate(object state, bool timedOut) { AsyncAssertState st = (AsyncAssertState)state; System.Diagnostics.Debug.WriteLine("Executing assert on thread: " + Thread.CurrentThread.GetHashCode()); lock (st.Builder) { st.Builder.Append(EvaluateAssert(st.Assert, st.Context)); } }
/// <summary> /// Callback for asynchonous delegate execution. /// </summary> /// <remarks> /// This is the <see cref="AsyncCallback"/> to use when executing /// asynchronously the <see cref="EvaluateAssert"/> method. /// It completes the call by calling EndInvoke, retrieving the results /// and appending the messages to the <see cref="StringBuilder"/> received /// in the <see cref="IAsyncResult.AsyncState"/> property. /// </remarks> /// <example> /// This in an example of the delegate creation and asynchronous execution. /// <code>AsyncAssertEvaluate eval = new AsyncAssertEvaluate(EvaluateAssert); /// eval.BeginInvoke(asr, ctx, /// new AsyncCallback(OnAssertCompleted), sb); /// </code> /// </example> /// <param name="result">The object to extract state information from.</param> private void OnAssertCompleted(IAsyncResult result) { try { AsyncResult ar = (AsyncResult)result; AsyncAssertState st = (AsyncAssertState)ar.AsyncState; AsyncAssertEvaluate eval = (AsyncAssertEvaluate)ar.AsyncDelegate; string res = eval.EndInvoke(ar); if (res != String.Empty) { lock (st.Builder) { st.Builder.Append(res).Append(System.Environment.NewLine); System.Diagnostics.Debug.WriteLine(res); } } } catch (Exception ex) { System.Diagnostics.Debug.Fail(ex.ToString()); throw ex; } }