NanoByte.Common  2.7.0
NanoByte.Common.CancellationGuard Class Reference

Ensures that a block of code running on a background thread cleanly exits before a CancellationTokenSource.Cancel() call completes. More...

Inheritance diagram for NanoByte.Common.CancellationGuard:

Public Member Functions

 CancellationGuard (CancellationToken cancellationToken)
 Registers a callback for the cancellationToken that blocks calls to CancellationTokenSource.Cancel() until Dispose has been called. More...
 
 CancellationGuard (CancellationToken cancellationToken, TimeSpan timeout)
 Registers a callback for the cancellationToken that blocks calls to CancellationTokenSource.Cancel() until Dispose has been called. More...
 
void Dispose ()
 Releases the block and allows CancellationTokenSource.Cancel() to complete. More...
 

Detailed Description

Ensures that a block of code running on a background thread cleanly exits before a CancellationTokenSource.Cancel() call completes.

Do not use this if CancellationTokenSource.Cancel() is called from the same SynchronizationContext the guarded code is running under. This could lead to deadlocks.

This class is best used in a using-block:

using (new CancellationGuard(cancellationToken))
{
// Your code
}
CancellationGuard(CancellationToken cancellationToken)
Registers a callback for the cancellationToken that blocks calls to CancellationTokenSource....
Definition: CancellationGuard.cs:36

Constructor & Destructor Documentation

◆ CancellationGuard() [1/2]

NanoByte.Common.CancellationGuard.CancellationGuard ( CancellationToken  cancellationToken)
inline

Registers a callback for the cancellationToken that blocks calls to CancellationTokenSource.Cancel() until Dispose has been called.

Parameters
cancellationTokenUsed to signal cancellation requests.

◆ CancellationGuard() [2/2]

NanoByte.Common.CancellationGuard.CancellationGuard ( CancellationToken  cancellationToken,
TimeSpan  timeout 
)
inline

Registers a callback for the cancellationToken that blocks calls to CancellationTokenSource.Cancel() until Dispose has been called.

Parameters
cancellationTokenUsed to signal cancellation requests.
timeoutA timespan after which the cancellation will be considered completed even if Dispose has not been called yet.

Member Function Documentation

◆ Dispose()

void NanoByte.Common.CancellationGuard.Dispose ( )
inline

Releases the block and allows CancellationTokenSource.Cancel() to complete.


The documentation for this class was generated from the following file: