<PackageReference Include="Nito.AsyncEx.Tasks" Version="5.1.2" />

Nito.AsyncEx.SynchronizationContextSwitcher

public sealed class SynchronizationContextSwitcher : SingleDisposable<object>
Utility class for temporarily switching SynchronizationContext implementations.
public static void ApplyContext(SynchronizationContext context, Action action)

Executes a synchronous delegate with the specified SynchronizationContext as "current". The previous current context is restored when this function returns.

public static T ApplyContext<T>(SynchronizationContext context, Func<T> action)

Executes a synchronous or asynchronous delegate without the specified SynchronizationContext as "current". The previous current context is restored when this function synchronously returns.

public static void NoContext(Action action)

Executes a synchronous delegate without the current SynchronizationContext. The current context is restored when this function returns.

public static T NoContext<T>(Func<T> action)

Executes a synchronous or asynchronous delegate without the current SynchronizationContext. The current context is restored when this function synchronously returns.