Class CancellationGuard
Ensures that a block of code running on a background thread cleanly exits before a Cancel() call completes.
Namespace: NanoByte.Common.Threading
Assembly: NanoByte.Common.dll
Syntax
public class CancellationGuard : Object
Remarks
Do not use this if Cancel() is called from the same SynchronizationContext the guarded code is running under. This could lead to deadlocks.
Examples
This class is best used in a using-block:
using (new CancellationGuard(cancellationToken))
{
// Your code
}
Constructors
CancellationGuard(CancellationToken)
Registers a callback for the cancellationToken
that blocks calls to Cancel() until Dispose() has been called.
Declaration
public CancellationGuard(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken | Used to signal cancellation requests. |
CancellationGuard(CancellationToken, TimeSpan)
Registers a callback for the cancellationToken
that blocks calls to Cancel() until Dispose() has been called.
Declaration
public CancellationGuard(CancellationToken cancellationToken, TimeSpan timeout)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken | Used to signal cancellation requests. |
TimeSpan | timeout | A timespan after which the cancellation will be considered completed even if Dispose() has not been called yet. |
Methods
Dispose()
Releases the block and allows Cancel() to complete.
Declaration
public void Dispose()