Table of Contents

Class ThreadingTools

Namespace
Microsoft.VisualStudio.Threading
Assembly
Microsoft.VisualStudio.Threading.dll

Utility methods for working across threads.

public static class ThreadingTools
Inheritance
ThreadingTools
Inherited Members

Methods

Apply(SynchronizationContext?, bool)

Applies the specified SynchronizationContext to the caller's context.

public static SpecializedSyncContext Apply(this SynchronizationContext? syncContext, bool checkForChangesOnRevert = true)

Parameters

syncContext SynchronizationContext

The synchronization context to apply.

checkForChangesOnRevert bool

A value indicating whether to check that the applied SyncContext is still the current one when the original is restored.

Returns

SpecializedSyncContext

ApplyChangeOptimistically<T>(ref T, Func<T, T>)

Optimistically performs some value transformation based on some field and tries to apply it back to the field, retrying as many times as necessary until no other thread is manipulating the same field.

public static bool ApplyChangeOptimistically<T>(ref T hotLocation, Func<T, T> applyChange) where T : class?

Parameters

hotLocation T

The field that may be manipulated by multiple threads.

applyChange Func<T, T>

A function that receives the unchanged value and returns the changed value.

Returns

bool

true if the location's value is changed by applying the result of the applyChange function; false if the location's value remained the same because the last invocation of applyChange returned the existing value.

Type Parameters

T

The type of data.

ApplyChangeOptimistically<T, TArg>(ref T, TArg, Func<T, TArg, T>)

Optimistically performs some value transformation based on some field and tries to apply it back to the field, retrying as many times as necessary until no other thread is manipulating the same field.

public static bool ApplyChangeOptimistically<T, TArg>(ref T hotLocation, TArg applyChangeArgument, Func<T, TArg, T> applyChange) where T : class?

Parameters

hotLocation T

The field that may be manipulated by multiple threads.

applyChangeArgument TArg

An argument to pass to applyChange.

applyChange Func<T, TArg, T>

A function that receives both the unchanged value and applyChangeArgument, then returns the changed value.

Returns

bool

true if the location's value is changed by applying the result of the applyChange function; false if the location's value remained the same because the last invocation of applyChange returned the existing value.

Type Parameters

T

The type of data to apply the change to.

TArg

The type of argument passed to the applyChange.

Remarks

Use this overload when applyChange requires a single item, as is common when updating immutable collection types. By passing the item as a method operand, the caller may be able to avoid allocating a closure object for every call.

WithCancellation(Task, CancellationToken)

Wraps a task with one that will complete as cancelled based on a cancellation token, allowing someone to await a task but be able to break out early by cancelling the token.

public static Task WithCancellation(this Task task, CancellationToken cancellationToken)

Parameters

task Task

The task to wrap.

cancellationToken CancellationToken

The token that can be canceled to break out of the await.

Returns

Task

The wrapping task.

WithCancellation<T>(Task<T>, CancellationToken)

Wraps a task with one that will complete as cancelled based on a cancellation token, allowing someone to await a task but be able to break out early by cancelling the token.

public static Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)

Parameters

task Task<T>

The task to wrap.

cancellationToken CancellationToken

The token that can be canceled to break out of the await.

Returns

Task<T>

The wrapping task.

Type Parameters

T

The type of value returned by the task.