CancellationToken.Register 方法

定義

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

多載

Register(Action)

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

Register(Action, Boolean)

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

Register(Action<Object>, Object)

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

Register(Action<Object>, Object, Boolean)

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

Register(Action)

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

public:
 System::Threading::CancellationTokenRegistration Register(Action ^ callback);
public System.Threading.CancellationTokenRegistration Register (Action callback);
member this.Register : Action -> System.Threading.CancellationTokenRegistration
Public Function Register (callback As Action) As CancellationTokenRegistration

參數

callback
Action

取消 CancellationToken 時所要執行的委派。The delegate to be executed when the CancellationToken is canceled.

傳回

可用來取消註冊回呼的 CancellationTokenRegistration 執行個體。The CancellationTokenRegistration instance that can be used to unregister the callback.

例外狀況

相關聯的 CancellationTokenSource 已經處置。The associated CancellationTokenSource has been disposed.

callback 為 null。callback is null.

備註

如果這個 token 已經是已取消的狀態,則會立即和同步執行委派。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.

目前的 ExecutionContext 會與委派一起捕捉,並會在執行時使用。The current ExecutionContext is captured along with the delegate and will be used when executing it.

未捕捉到目前的 SynchronizationContextThe current SynchronizationContext is not captured.

另請參閱

Register(Action, Boolean)

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

public:
 System::Threading::CancellationTokenRegistration Register(Action ^ callback, bool useSynchronizationContext);
public System.Threading.CancellationTokenRegistration Register (Action callback, bool useSynchronizationContext);
member this.Register : Action * bool -> System.Threading.CancellationTokenRegistration
Public Function Register (callback As Action, useSynchronizationContext As Boolean) As CancellationTokenRegistration

參數

callback
Action

取消 CancellationToken 時所要執行的委派。The delegate to be executed when the CancellationToken is canceled.

useSynchronizationContext
Boolean

指示是否要擷取目前的 SynchronizationContext 並在叫用 callback 時使用它的值。A value that indicates whether to capture the current SynchronizationContext and use it when invoking the callback.

傳回

可用來取消註冊回呼的 CancellationTokenRegistration 執行個體。The CancellationTokenRegistration instance that can be used to unregister the callback.

例外狀況

相關聯的 CancellationTokenSource 已經處置。The associated CancellationTokenSource has been disposed.

callback 為 null。callback is null.

備註

如果這個 token 已經是已取消的狀態,則會立即和同步執行委派。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.

目前的 ExecutionContext 會與委派一起捕捉,並會在執行時使用。The current ExecutionContext is captured along with the delegate and will be used when executing it.

如果 useSynchronizationContexttrue,則目前的 SynchronizationContext (如果有的話)也會與委派一起捕捉,並在執行時使用。If useSynchronizationContext is true, the current SynchronizationContext, if one exists, is also captured along with the delegate and will be used when executing it. 否則,不會捕捉 SynchronizationContextOtherwise, SynchronizationContext is not captured.

另請參閱

Register(Action<Object>, Object)

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

public:
 System::Threading::CancellationTokenRegistration Register(Action<System::Object ^> ^ callback, System::Object ^ state);
public System.Threading.CancellationTokenRegistration Register (Action<object> callback, object state);
member this.Register : Action<obj> * obj -> System.Threading.CancellationTokenRegistration
Public Function Register (callback As Action(Of Object), state As Object) As CancellationTokenRegistration

參數

callback
Action<Object>

取消 CancellationToken 時所要執行的委派。The delegate to be executed when the CancellationToken is canceled.

state
Object

當叫用委派時,要傳遞給 callback 的狀態。The state to pass to the callback when the delegate is invoked. 這可能是 null。This may be null.

傳回

可用來取消註冊回呼的 CancellationTokenRegistration 執行個體。The CancellationTokenRegistration instance that can be used to unregister the callback.

例外狀況

相關聯的 CancellationTokenSource 已經處置。The associated CancellationTokenSource has been disposed.

callback 為 null。callback is null.

備註

如果這個 token 已經是已取消的狀態,則會立即和同步執行委派。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.

目前的 ExecutionContext 會與委派一起捕捉,並會在執行時使用。The current ExecutionContext is captured along with the delegate and will be used when executing it.

未捕捉到目前的 SynchronizationContextThe current SynchronizationContext is not captured.

另請參閱

Register(Action<Object>, Object, Boolean)

註冊委派,當取消這個 CancellationToken 時將會呼叫該委派。Registers a delegate that will be called when this CancellationToken is canceled.

public:
 System::Threading::CancellationTokenRegistration Register(Action<System::Object ^> ^ callback, System::Object ^ state, bool useSynchronizationContext);
public System.Threading.CancellationTokenRegistration Register (Action<object> callback, object state, bool useSynchronizationContext);
member this.Register : Action<obj> * obj * bool -> System.Threading.CancellationTokenRegistration
Public Function Register (callback As Action(Of Object), state As Object, useSynchronizationContext As Boolean) As CancellationTokenRegistration

參數

callback
Action<Object>

取消 CancellationToken 時所要執行的委派。The delegate to be executed when the CancellationToken is canceled.

state
Object

當叫用委派時,要傳遞給 callback 的狀態。The state to pass to the callback when the delegate is invoked. 這可能是 null。This may be null.

useSynchronizationContext
Boolean

布林值,表示是否要擷取目前的 SynchronizationContext 並在叫用 callback 時使用它。A Boolean value that indicates whether to capture the current SynchronizationContext and use it when invoking the callback.

傳回

可用來取消註冊回呼的 CancellationTokenRegistration 執行個體。The CancellationTokenRegistration instance that can be used to unregister the callback.

例外狀況

相關聯的 CancellationTokenSource 已經處置。The associated CancellationTokenSource has been disposed.

callback 為 null。callback is null.

備註

如果這個 token 已經是已取消的狀態,則會立即和同步執行委派。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.

目前的 ExecutionContext 會與委派一起捕捉,並會在執行時使用。The current ExecutionContext is captured along with the delegate and will be used when executing it.

如果 useSynchronizationContexttrue,則目前的 SynchronizationContext (如果有的話)也會與委派一起捕捉,並在執行時使用。If useSynchronizationContext is true, the current SynchronizationContext, if one exists, is also captured along with the delegate and will be used when executing it. 否則,不會捕捉 SynchronizationContextOtherwise, SynchronizationContext is not captured.

另請參閱

適用於