/// <summary> /// Asynchronous evaluation of a <see cref="Report"/> /// </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="AsyncReportState"/> and executes the /// <see cref="EvaluateReport"/> 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 OnReportEvaluate(object state, bool timedOut) { AsyncReportState st = (AsyncReportState)state; System.Diagnostics.Debug.WriteLine("Executing report on thread: " + Thread.CurrentThread.GetHashCode()); lock (st.Builder) { st.Builder.Append(EvaluateReport(st.Report, st.Context)); } }
/// <summary> /// Callback for asynchonous delegate execution. /// </summary> /// <remarks> /// This is the <see cref="AsyncCallback"/> to use when executing /// asynchronously the <see cref="EvaluateReport"/> 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>AsyncReportEvaluate eval = new AsyncReportEvaluate(EvaluateReport); /// eval.BeginInvoke(rpt, ctx, new AsyncCallback(OnReportCompleted), sb); /// </code> /// </example> /// <param name="result">The object to extract state information from.</param> private void OnReportCompleted(IAsyncResult result) { try { AsyncResult ar = (AsyncResult)result; AsyncReportState st = (AsyncReportState)ar.AsyncState; AsyncReportEvaluate eval = (AsyncReportEvaluate)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; } }