/// <summary> /// Execute the function to capture the run. /// </summary> /// <typeparam name="TU">Result type of the function.</typeparam> /// <param name="function"> /// <see cref="Action"/> to be analyzed. /// </param> /// <returns> /// Return <see cref="RunTrace"/> describing the execution. /// </returns> internal static RunTraceResult <TU> GetTrace <TU>(Func <TU> function) { var result = new RunTraceResult <TU>(); CaptureTrace(() => { result.Result = function(); #if !DOTNET_20 && !DOTNET_30 && !DOTNET_35 && !DOTNET_40 if (!(result.Result is Task ta)) { return; } // we must check if the method is flagged async if (!FunctionIsAsync(function)) { return; } try { ta.Wait(); } catch (AggregateException exception) { result.RaisedException = exception.InnerException; } #endif }, result); return(result); }
/// <summary> /// Execute the function to capture the run. /// </summary> /// <typeparam name="TU">Result type of the function.</typeparam> /// <param name="function"> /// <see cref="Action"/> to be analyzed. /// </param> /// <returns> /// Return <see cref="RunTrace"/> describing the execution. /// </returns> internal static RunTraceResult <TU> GetTrace <TU>(Func <TU> function) { var result = new RunTraceResult <TU>(); CaptureTrace(() => result.Result = function(), result); return(result); }
/// <summary> /// Execute the function to capture the run. /// </summary> /// <typeparam name="TResult">Result type of the awaitable function.</typeparam> /// <param name="awaitableFunction"> /// <see cref="Action"/> to be analyzed. /// </param> /// <returns> /// Return <see cref="RunTrace"/> describing the execution. /// </returns> internal static RunTraceResult <TResult> GetAsyncTrace <TResult>(Func <Task <TResult> > awaitableFunction) { var result = new RunTraceResult <TResult>(); CaptureAsyncTrace(awaitableFunction, result); return(result); }
/// <summary> /// Execute the function to capture the run. /// </summary> /// <typeparam name="TResult">Result type of the awaitable function.</typeparam> /// <param name="waitableFunction"> /// <see cref="Action"/> to be analyzed. /// </param> /// <returns> /// Return <see cref="RunTrace"/> describing the execution. /// </returns> internal static RunTraceResult <TResult> GetAsyncTrace <TResult>(Func <Task <TResult> > waitableFunction) { var result = new RunTraceResult <TResult>(); CaptureTrace( () => { try { // starts and waits the completion of the awaitable method result.Result = waitableFunction().Result; } catch (AggregateException agex) { result.RaisedException = agex.InnerException; } }, result); return(result); }
private static void CaptureAsyncTrace <TResult>(Func <Task <TResult> > awaitableFunction, RunTraceResult <TResult> result) { var watch = new Stopwatch(); var cpu = Process.GetCurrentProcess().TotalProcessorTime; try { watch.Start(); // starts and waits the completion of the awaitable method awaitableFunction().Wait(); result.Result = awaitableFunction().Result; } catch (AggregateException agex) { result.RaisedException = agex.InnerException; } finally { watch.Stop(); result.TotalProcessorTime = Process.GetCurrentProcess().TotalProcessorTime - cpu; } // AFAIK, ObjectDisposedException should never happen here // ReSharper disable PossibleLossOfFraction result.ExecutionTime = TimeSpan.FromTicks(watch.ElapsedTicks); }