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
syncContextSynchronizationContextThe synchronization context to apply.
checkForChangesOnRevertboolA value indicating whether to check that the applied SyncContext is still the current one when the original is restored.
Returns
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
hotLocationTThe field that may be manipulated by multiple threads.
applyChangeFunc<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
applyChangefunction; false if the location's value remained the same because the last invocation ofapplyChangereturned the existing value.
Type Parameters
TThe 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
hotLocationTThe field that may be manipulated by multiple threads.
applyChangeArgumentTArgAn argument to pass to
applyChange.applyChangeFunc<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
applyChangefunction; false if the location's value remained the same because the last invocation ofapplyChangereturned the existing value.
Type Parameters
TThe type of data to apply the change to.
TArgThe 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
taskTaskThe task to wrap.
cancellationTokenCancellationTokenThe 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
taskTask<T>The task to wrap.
cancellationTokenCancellationTokenThe token that can be canceled to break out of the await.
Returns
- Task<T>
The wrapping task.
Type Parameters
TThe type of value returned by the task.