Class AsyncContext
- Namespace
- ReactiveUI.Extensions.Async
- Assembly
- ReactiveUI.Extensions.dll
Represents an asynchronous execution context that encapsulates a specific SynchronizationContext or TaskScheduler for controlling the scheduling of asynchronous operations.
public record AsyncContext : IEquatable<AsyncContext>
- Inheritance
-
AsyncContext
- Implements
- Extension Methods
Remarks
Use AsyncContext to capture and restore a particular synchronization or task scheduling environment when running asynchronous code. This is useful for ensuring that continuations or asynchronous callbacks execute on a desired context, such as a UI thread or a custom scheduler. An AsyncContext can be created from either a SynchronizationContext or a TaskScheduler, but not both at the same time. The Default context represents the absence of a specific synchronization or scheduling context, and typically corresponds to the default task scheduler.
Constructors
AsyncContext(AsyncContext)
protected AsyncContext(AsyncContext original)
Parameters
originalAsyncContext
Properties
Default
Gets the default instance of the AsyncContext class.
public static AsyncContext Default { get; }
Property Value
Remarks
Use this property to access a shared, default AsyncContext instance when a custom context is not required.
EqualityContract
protected virtual Type EqualityContract { get; }
Property Value
SynchronizationContext
Gets the synchronization context to use for marshaling callbacks and continuations.
public SynchronizationContext? SynchronizationContext { get; init; }
Property Value
Remarks
If this property is set, callbacks and continuations will be posted to the specified synchronization context. If null, the default context is used, which may result in execution on a thread pool thread. This property is typically used to ensure that asynchronous operations resume on a specific thread or context, such as a UI thread.
TaskScheduler
Gets the task scheduler to use for scheduling tasks, or null to use the default scheduler.
public TaskScheduler? TaskScheduler { get; init; }
Property Value
Methods
Equals(AsyncContext?)
public virtual bool Equals(AsyncContext? other)
Parameters
otherAsyncContext
Returns
Equals(object?)
public override bool Equals(object? obj)
Parameters
objobject
Returns
From(IScheduler)
Creates a new AsyncContext using the specified scheduler for task and synchronization context management.
public static AsyncContext From(IScheduler scheduler)
Parameters
schedulerISchedulerThe scheduler to use for configuring the AsyncContext.
Returns
- AsyncContext
An AsyncContext instance configured with the provided scheduler.
Remarks
If the provided scheduler directly implements SynchronizationContext or TaskScheduler, those instances are used directly. Otherwise, the scheduler is wrapped in a TaskScheduler adapter that delegates task execution to the scheduler.
Exceptions
- ArgumentNullException
Thrown if scheduler is null.
From(SynchronizationContext)
Creates a new AsyncContext that uses the specified SynchronizationContext for asynchronous operations.
public static AsyncContext From(SynchronizationContext synchronizationContext)
Parameters
synchronizationContextSynchronizationContextThe SynchronizationContext to associate with the AsyncContext. Cannot be null.
Returns
- AsyncContext
An AsyncContext instance configured to use the provided SynchronizationContext.
Remarks
The returned AsyncContext will have its TaskScheduler property set to null. Use this method when you want to control asynchronous execution using a specific SynchronizationContext, such as for UI thread synchronization.
Exceptions
- ArgumentNullException
Thrown if synchronizationContext is null.
From(TaskScheduler)
Creates a new AsyncContext that uses the specified TaskScheduler for task execution.
public static AsyncContext From(TaskScheduler taskScheduler)
Parameters
taskSchedulerTaskSchedulerThe TaskScheduler to associate with the new AsyncContext. Cannot be null.
Returns
- AsyncContext
An AsyncContext instance configured to use the specified TaskScheduler. The SynchronizationContext property of the returned instance is set to null.
Exceptions
- ArgumentNullException
Thrown if taskScheduler is null.
GetCurrent()
Gets the current asynchronous context associated with the calling thread.
public static AsyncContext GetCurrent()
Returns
- AsyncContext
An AsyncContext representing the current asynchronous context. If a SynchronizationContext is present, it is used; otherwise, the current TaskScheduler is used.
Remarks
Use this method to capture the context for scheduling asynchronous operations that should continue on the same logical thread or synchronization context. This is commonly used to ensure code executes on the appropriate context, such as a UI thread in desktop applications.
GetHashCode()
public override int GetHashCode()
Returns
PrintMembers(StringBuilder)
protected virtual bool PrintMembers(StringBuilder builder)
Parameters
builderStringBuilder
Returns
SwitchContextAsync(bool, CancellationToken)
Creates an awaitable that switches execution to the associated asynchronous context.
public AsyncContext.AsyncContextSwitcherAwaitable SwitchContextAsync(bool forceYielding, CancellationToken cancellationToken)
Parameters
forceYieldingbooltrue to always yield execution to the context, even if already in the correct context; otherwise, false to avoid yielding if already in the context.
cancellationTokenCancellationTokenA cancellation token that can be used to cancel the context switch operation.
Returns
- AsyncContext.AsyncContextSwitcherAwaitable
An awaitable that completes when execution has switched to the asynchronous context.
ToString()
public override string ToString()
Returns
Operators
operator ==(AsyncContext?, AsyncContext?)
public static bool operator ==(AsyncContext? left, AsyncContext? right)
Parameters
leftAsyncContextrightAsyncContext
Returns
operator !=(AsyncContext?, AsyncContext?)
public static bool operator !=(AsyncContext? left, AsyncContext? right)
Parameters
leftAsyncContextrightAsyncContext