Skip to content

[API Proposal]: Add Parallel.InvokeAsync #112900

@bill-poole

Description

@bill-poole

Background and motivation

Parallel.Invoke currently provides for concurrently executing a set of Actions, but does not provide for executing a set of async actions (Func<CancellationToken, ValueTask>). I have found a frequent need to concurrently execute a set of async actions, where if any action fails, all other actions are cancelled and the exception of the failing action is thrown. i.e., similar behavior is required that is provided by Parallel.ForEachAsync, but where ForEachAsync executes a single function for a collection of inputs, Parallel.InvokeAsync would execute a collection of async actions.

API Proposal

namespace System.Threading.Tasks public static class Parallel { public static Task InvokeAsync(params Func<CancellationToken, ValueTask>[] actions); public static Task InvokeAsync(CancellationToken cancellationToken, params Func<CancellationToken, ValueTask>[] actions); public static Task InvokeAsync(ParallelOptions parallelOptions, params Func<CancellationToken, ValueTask> actions); }

API Usage

await Parallel.InvokeAsync( async ct => obj.Id = new(obj.Id.ExternalId, internalId: await idMapper.GetInternalId(obj.Id.ExternalId, ct)), async ct => obj.ParentId = new(obj.Id.ExternalId, internalId: await idMapper.GetInternalId(obj.ParentId.ExternalId, ct)), async ct => obj.OrderId = new(obj.Id.ExternalId, internalId: await idMapper.GetInternalId(obj.OrderId.ExternalId, ct)), async ct => obj.AssignmentId = new(obj.Id.ExternalId, internalId: await idMapper.GetInternalId(obj.AssignmentId.ExternalId, ct)));

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions