CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
System::Threading::CancellationToken Class Reference

Propagates notification that operations should be canceled. This class provides a mechanism for cooperative cancellation between threads, allowing one thread to notify others that an operation should be canceled. More...

#include <cancellation_token.h>

Inherits System::Details::BoxableObjectBase.

Public Member Functions

 CancellationToken ()
 Default constructor. More...
 
bool get_IsCancellationRequested () const
 Gets whether cancellation has been requested for this token. More...
 
bool get_CanBeCanceled () const
 Gets whether this token is capable of being in the canceled state. More...
 
CancellationTokenRegistration Register (const Action<> &callback) const
 Registers a callback that will be invoked when cancellation is requested. More...
 
void ThrowIfCancellationRequested () const
 Throws a OperationCanceledException if cancellation has been requested. More...
 

Static Public Member Functions

static CancellationToken get_None ()
 Returns an empty System::Threading::CancellationToken value. More...
 

Detailed Description

Propagates notification that operations should be canceled. This class provides a mechanism for cooperative cancellation between threads, allowing one thread to notify others that an operation should be canceled.

Note
A CancellationToken can only be canceled through its associated CancellationTokenSource.

Constructor & Destructor Documentation

◆ CancellationToken()

System::Threading::CancellationToken::CancellationToken ( )

Default constructor.

Creates a token that will never be canceled (equivalent to None).

Member Function Documentation

◆ get_CanBeCanceled()

bool System::Threading::CancellationToken::get_CanBeCanceled ( ) const

Gets whether this token is capable of being in the canceled state.

Returns
true if this token is capable of being in the canceled state; otherwise, false.
Note
Tokens created from CancellationTokenSource will return true, while the None token will always return false.

◆ get_IsCancellationRequested()

bool System::Threading::CancellationToken::get_IsCancellationRequested ( ) const

Gets whether cancellation has been requested for this token.

Returns
true if cancellation has been requested for this token; otherwise, false.

◆ get_None()

static CancellationToken System::Threading::CancellationToken::get_None ( )
static

Returns an empty System::Threading::CancellationToken value.

Returns
An empty cancellation token that cannot be canceled.
Note
The returned token will never transition to a canceled state.

◆ Register()

CancellationTokenRegistration System::Threading::CancellationToken::Register ( const Action<> &  callback) const

Registers a callback that will be invoked when cancellation is requested.

Parameters
callbackThe Action<> to execute when cancellation is requested.
Returns
A CancellationTokenRegistration object that can be used to deregister the callback.
Note
If cancellation has already been requested, the callback will be invoked immediately.
Warning
The callback should be short-lived and non-blocking as it will be executed on the thread that calls Cancel() on the CancellationTokenSource.

◆ ThrowIfCancellationRequested()

void System::Threading::CancellationToken::ThrowIfCancellationRequested ( ) const

Throws a OperationCanceledException if cancellation has been requested.

Exceptions
OperationCanceledExceptionif get_IsCancellationRequested is true.
Note
This method provides a convenient way to check for cancellation at specific points in your code where it's safe to throw an exception.