Table of Contents

Struct AsyncContext.AsyncContextSwitcherAwaitable

Namespace
ReactiveUI.Extensions.Async
Assembly
ReactiveUI.Extensions.dll

Provides an awaitable that switches execution to a specified asynchronous context, optionally forcing a yield and supporting cancellation.

public readonly struct AsyncContext.AsyncContextSwitcherAwaitable : INotifyCompletion, IEquatable<AsyncContext.AsyncContextSwitcherAwaitable>
Implements
Extension Methods

Remarks

Use this struct to ensure that code after an await resumes on a specific asynchronous context, such as a particular SynchronizationContext or TaskScheduler. If cancellation is requested before the continuation is scheduled, the continuation is invoked immediately and an OperationCanceledException will be thrown when GetResult is called. This type is intended for advanced scenarios where precise control over asynchronous context switching is required.

Constructors

AsyncContextSwitcherAwaitable(AsyncContext, bool, CancellationToken)

Provides an awaitable that switches execution to a specified asynchronous context, optionally forcing a yield and supporting cancellation.

public AsyncContextSwitcherAwaitable(AsyncContext AsyncContext, bool ForceYielding, CancellationToken CancellationToken)

Parameters

AsyncContext AsyncContext

The asynchronous context to which execution should be switched when awaited.

ForceYielding bool

true to always yield execution even if already in the target context; otherwise, false to avoid yielding if already in the specified context.

CancellationToken CancellationToken

A cancellation token that can be used to cancel the await operation before the continuation is scheduled.

Remarks

Use this struct to ensure that code after an await resumes on a specific asynchronous context, such as a particular SynchronizationContext or TaskScheduler. If cancellation is requested before the continuation is scheduled, the continuation is invoked immediately and an OperationCanceledException will be thrown when GetResult is called. This type is intended for advanced scenarios where precise control over asynchronous context switching is required.

Properties

AsyncContext

The asynchronous context to which execution should be switched when awaited.

public AsyncContext AsyncContext { get; init; }

Property Value

AsyncContext

CancellationToken

A cancellation token that can be used to cancel the await operation before the continuation is scheduled.

public CancellationToken CancellationToken { get; init; }

Property Value

CancellationToken

ForceYielding

true to always yield execution even if already in the target context; otherwise, false to avoid yielding if already in the specified context.

public bool ForceYielding { get; init; }

Property Value

bool

IsCompleted

Gets a value indicating whether the asynchronous operation has completed in the current context.

public bool IsCompleted { get; }

Property Value

bool

Methods

Deconstruct(out AsyncContext, out bool, out CancellationToken)

public void Deconstruct(out AsyncContext AsyncContext, out bool ForceYielding, out CancellationToken CancellationToken)

Parameters

AsyncContext AsyncContext
ForceYielding bool
CancellationToken CancellationToken

Equals(AsyncContextSwitcherAwaitable)

public bool Equals(AsyncContext.AsyncContextSwitcherAwaitable other)

Parameters

other AsyncContext.AsyncContextSwitcherAwaitable

Returns

bool

Equals(object)

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

GetAwaiter()

Returns an awaiter for this AsyncContextSwitcherAwaitable instance, enabling use of the await keyword to asynchronously switch execution context.

public AsyncContext.AsyncContextSwitcherAwaitable GetAwaiter()

Returns

AsyncContext.AsyncContextSwitcherAwaitable

An awaiter that can be used to await this instance and perform an asynchronous context switch.

GetHashCode()

public override int GetHashCode()

Returns

int

GetResult()

Checks whether the associated cancellation token has had cancellation requested and throws an exception if so.

public void GetResult()

Remarks

This method is typically used to observe cancellation requests and respond by throwing an OperationCanceledException if cancellation has been signaled. If cancellation has not been requested, the method returns normally.

OnCompleted(Action)

Schedules the specified continuation action to be invoked when the operation has completed.

public void OnCompleted(Action continuation)

Parameters

continuation Action

The action to execute when the operation is complete. Cannot be null.

Remarks

If a synchronization context is available, the continuation is posted to it; otherwise, the continuation is scheduled on the associated task scheduler or the default task scheduler. If the operation has already been canceled, the continuation is invoked immediately on the current thread.

ToString()

public override string ToString()

Returns

string

Operators

operator ==(AsyncContextSwitcherAwaitable, AsyncContextSwitcherAwaitable)

public static bool operator ==(AsyncContext.AsyncContextSwitcherAwaitable left, AsyncContext.AsyncContextSwitcherAwaitable right)

Parameters

left AsyncContext.AsyncContextSwitcherAwaitable
right AsyncContext.AsyncContextSwitcherAwaitable

Returns

bool

operator !=(AsyncContextSwitcherAwaitable, AsyncContextSwitcherAwaitable)

public static bool operator !=(AsyncContext.AsyncContextSwitcherAwaitable left, AsyncContext.AsyncContextSwitcherAwaitable right)

Parameters

left AsyncContext.AsyncContextSwitcherAwaitable
right AsyncContext.AsyncContextSwitcherAwaitable

Returns

bool