public sealed class AcceptedWorkScope : IAsyncDisposable Owns accepted application tasks until their actual completion.
Remarks
Use one instance for the application lifetime that owns the work, such as a scoped model. Register the complete task, including recovery and cleanup, before starting it. This scope does not serialize work, dispatch it to a model context, or request cancellation.
Constructors
AcceptedWorkScope
public AcceptedWorkScope() Creates a scope that accepts work until draining begins.
Remarks
Failures of accepted tasks are written to Trace.
Methods
DisposeAsync
public ValueTask DisposeAsync() Stops accepting work and drains it without requesting cancellation.
Remarks
Concurrent and repeated disposal wait for the same completion. Disposal from accepted work of this scope does not wait for the calling work; see AcceptedWorkScope.DrainAsync.
DrainAsync
public Task DrainAsync(CancellationToken cancellationToken = default) Stops accepting work and waits for all accepted factories and tasks to finish.
Remarks
Cancellation only stops this caller's wait; admission remains closed and accepted work remains owned. Each returned task retains its original outcome. A faulted task is logged once (event 1013) without failing the drain; cancellations are not logged. Call again or dispose asynchronously to await completion after a cancelled wait.
When called from accepted work of this scope, including code that inherits its execution context, the drain waits for all other accepted work but not for the calling work, which cannot finish while it awaits its own drain. Work such as "Save and close" can therefore dispose its owner. Code that runs in that work after the drain must not rely on resources the owner releases.
RunAsync
public Task RunAsync(Func<Task> work) Accepts ownership before invoking work and returns its actual task.
Exceptions
ArgumentNullExceptionThe factory is null.
InvalidOperationExceptionDraining has begun, or the factory returns null.
Remarks
The factory runs synchronously on the calling thread without holding a scope lock. A synchronous factory exception is rethrown to the caller. Cancellation of a separate invocation or observation task does not release ownership of the returned task.
RunAsync
public Task<TResult> RunAsync<TResult>(Func<Task<TResult>> work) Accepts ownership before invoking work and returns its actual result task.
Exceptions
ArgumentNullExceptionThe factory is null.
InvalidOperationExceptionDraining has begun, or the factory returns null.
Remarks
The factory runs synchronously on the calling thread without holding a scope lock. A synchronous factory exception is rethrown to the caller. Cancellation of a separate invocation or observation task does not release ownership of the returned task.
WaitForAcceptedAsync
public Task WaitForAcceptedAsync(CancellationToken cancellationToken = default) Waits until every task this scope accepted before the call has finished, and keeps accepting work: work accepted later is accepted as usual and not waited for.
Parameters
cancellationTokenStops only this caller's wait; admission and the accepted work are unaffected.
Exceptions
OperationCanceledExceptioncancellationTokenwas cancelled.
Remarks
Unlike AcceptedWorkScope.DrainAsync, it commits to nothing. A close guard can wait for the work and still keep its window open afterwards, with the scope accepting work as before. A Window drains a scope this way when its close waits for it (IRunicWindowContext.DrainOnClose).
When called from accepted work of this scope, including code that inherits its execution context, the wait does not wait for the calling work, as AcceptedWorkScope.DrainAsync does not. Faulted work is logged once (event 1013), as for every accepted task, without failing the wait. After draining began, the wait still waits only for the work accepted before the call.