public TaskScope(CancellationToken cancellationToken = default)Initializes a new TaskScope optionally linked to a parent cancellationToken.
cancellationToken- An optional parent token whose cancellation propagates to all child tasks.
All Types / Ferrous.Concurrency
public sealed class TaskScope : IAsyncDisposable
A structured concurrency nursery inspired by Rust's std::thread::scope and tokio::task::JoinSet. Guarantees that every child task spawned inside the scope either completes or is cooperatively cancelled and awaited before leaving the await using block—preventing orphaned background tasks and unobserved exceptions. Compiled with .NET 11 runtime async (runtime-async=on) on net11.0.
public TaskScope(CancellationToken cancellationToken = default)Initializes a new TaskScope optionally linked to a parent cancellationToken.
cancellationTokenpublic CancellationToken TokenGets the cooperative CancellationToken shared by all child tasks in this scope. Cancelled automatically when the parent token cancels, when any sibling task faults, when Cancel is called, or when the scope exits early.
public static async Task RunAsync(Func<TaskScope, Task> body, CancellationToken cancellationToken = default)Creates a new TaskScope and executes body within it, awaiting all spawned tasks (or cancelling and draining them on failure) before returning.
bodycancellationTokenpublic Task Spawn(Func<CancellationToken, Task> work)Spawns a child task tracked by this scope. If the child throws a non-cancellation exception, all sibling tasks in the scope are immediately signalled via Token.
workToken.ObjectDisposedExceptionpublic Task<T> Spawn<T>(Func<CancellationToken, Task<T>> work)Spawns a result-producing child task tracked by this scope. If the child throws a non-cancellation exception, all sibling tasks in the scope are immediately signalled via Token.
TworkToken.ObjectDisposedExceptionpublic void Cancel()Cooperatively cancels all tasks running inside this scope.
public async ValueTask WaitAllAsync()Awaits all tasks spawned in this scope (including tasks spawned dynamically by children) and rethrows the first non-cancellation failure if any child faulted.
public async ValueTask DisposeAsync()Cancels any still-running tasks (if WaitAllAsync was not called), awaits all spawned tasks so none leak past the scope boundary, and disposes resources.