static string ToString(AsyncContext context) => context?.Id.ToString(CultureInfo.InvariantCulture) ?? "x";
/// <summary> /// Starts an asynchronous task with a new <see cref="AsyncContext"/>. /// </summary> /// <param name="function">A function starting and returning an asynchronous task.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns>The asynchronous task that was started.</returns> /// <remarks> /// <para>This is equivalent to doing <c>return function(cancellationToken);</c> except that the task starts with a new <see cref="AsyncContext"/></para> /// </remarks> public static ValueTask <TResult> WithNewContext <TResult>(Func <CancellationToken, ValueTask <TResult> > function, CancellationToken cancellationToken) => AsyncContext.WithNewContextInternal(function, cancellationToken);
/// <summary> /// Starts an asynchronous task with a new <see cref="AsyncContext"/>. /// </summary> /// <param name="function">A function starting and returning an asynchronous task.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns>The asynchronous task that was started.</returns> /// <remarks> /// <para>This is equivalent to doing <c>return function(cancellationToken);</c> except that the task starts with a new <see cref="AsyncContext"/></para> /// </remarks> public static ValueTask WithNewContext(Func <CancellationToken, ValueTask> function, CancellationToken cancellationToken) => AsyncContext.WithNewContextInternal(function, cancellationToken);
/// <summary> /// Starts an asynchronous task with a new <see cref="AsyncContext"/>. /// </summary> /// <param name="function">A function starting and returning an asynchronous task.</param> /// <returns>The asynchronous task that was started.</returns> /// <remarks> /// <para>This is equivalent to doing <c>return function();</c> except that the task starts with a new <see cref="AsyncContext"/></para> /// </remarks> public static ValueTask <TResult> WithNewContext <TResult>(Func <ValueTask <TResult> > function) => AsyncContext.WithNewContextInternal(function);
// TODO: consider having overloads for arguments to avoid capturing // see ArgsVsCaptureAndStateMachine - it would be more efficient but requires a lot of code /// <summary> /// Starts an asynchronous task with a new <see cref="AsyncContext"/>. /// </summary> /// <param name="function">A function starting and returning an asynchronous task.</param> /// <returns>The asynchronous task that was started.</returns> /// <remarks> /// <para>This is equivalent to doing <c>return function();</c> except that the task starts with a new <see cref="AsyncContext"/></para> /// </remarks> public static ValueTask WithNewContext(Func <ValueTask> function) => AsyncContext.WithNewContextInternal(function);