CancellationToken.Register Method

Definition

Overloads

Register(Action)

Registers a delegate that will be called when this CancellationToken is canceled.

Register(Action, Boolean)

Registers a delegate that will be called when this CancellationToken is canceled.

Register(Action<Object>, Object)

Registers a delegate that will be called when this CancellationToken is canceled.

Register(Action<Object>, Object, Boolean)

Registers a delegate that will be called when this CancellationToken is canceled.

Register(Action)

Registers a delegate that will be called when this CancellationToken is canceled.

public System.Threading.CancellationTokenRegistration Register (Action callback);
Parameters
callback
Action

The delegate to be executed when the CancellationToken is canceled.

Returns

The CancellationTokenRegistration instance that can be used to deregister the callback.

Exceptions

The associated CancellationTokenSource has been disposed.

callback is null.

Remarks

If this token is already in the canceled state, the delegate will be run immediately and synchronously. Any exception the delegate generates will be propagated out of this method call.

The current ExecutionContext, if one exists, will be captured along with the delegate and will be used when executing it.

Register(Action, Boolean)

Registers a delegate that will be called when this CancellationToken is canceled.

public System.Threading.CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext);
Parameters
callback
Action

The delegate to be executed when the CancellationToken is canceled.

useSynchronizationContext
Boolean

A value that indicates whether to capture the current SynchronizationContext and use it when invoking the callback.

Returns

The CancellationTokenRegistration instance that can be used to deregister the callback.

Exceptions

The associated CancellationTokenSource has been disposed.

callback is null.

Remarks

If this token is already in the canceled state, the delegate will be run immediately and synchronously. Any exception the delegate generates will be propogated out of this method call.

If useSynchronizationContext is true, the current ExecutionContext, if one exists, will be captured along with the delegate and will be used when executing it.

Register(Action<Object>, Object)

Registers a delegate that will be called when this CancellationToken is canceled.

public System.Threading.CancellationTokenRegistration Register (Action<object> callback, object state);
Parameters
callback
Action<Object>

The delegate to be executed when the CancellationToken is canceled.

state
Object

The state to pass to the callback when the delegate is invoked. This may be null.

Returns

The CancellationTokenRegistration instance that can be used to deregister the callback.

Exceptions

The associated CancellationTokenSource has been disposed.

callback is null.

Remarks

If this token is already in the canceled state, the delegate will be run immediately and synchronously. Any exception the delegate generates will be propogated out of this method call.

The current ExecutionContext, if one exists, will be captured along with the delegate and will be used when executing it.

Register(Action<Object>, Object, Boolean)

Registers a delegate that will be called when this CancellationToken is canceled.

public System.Threading.CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext);
Parameters
callback
Action<Object>

The delegate to be executed when the CancellationToken is canceled.

state
Object

The state to pass to the callback when the delegate is invoked. This may be null.

useSynchronizationContext
Boolean

A Boolean value that indicates whether to capture the current SynchronizationContext and use it when invoking the callback.

Returns

The CancellationTokenRegistration instance that can be used to deregister the callback.

Exceptions

The associated CancellationTokenSource has been disposed.

callback is null.

Remarks

If this token is already in the canceled state, the delegate will be run immediately and synchronously. Any exception the delegate generates will be propogated out of this method call.

The current ExecutionContext, if one exists, will be captured along with the delegate and will be used when executing it.