The Cvb::CancellationToken
gives the possibility to cancel individual wait calls instead of the whole acquisition. Restarting the whole acquisition takes much longer, than to simply call wait functions again. A use case for example would be if an external stop signal is received by the application, the fastest reaction to stop the acquisition and to restart it, would be to abort the wait function with a cancellation token. The token itself can be checked if it has been canceled. In addition, on cancellation, the returned Cvb::WaitStatus
will have a value of Cvb::WaitStatus::Abort
.
When using a cancellation token, the following rules should be observed:
- The cancellation of the acquisition may not occur immediately and if an image is received before the cancellation is processed, a race condition may occur, and the cancellation may not be seen. It is recommended to check the cancellation token even if an image has been received to properly act on it.
- If multiple waits are cancelled at the same time through a single token, they may finish in a random order.
auto token = source.
Token();
std::tie(image, waitStatus, enumerator) = stream->Wait(*token);
token->IsCanceled();
CancellationTokenPtr Token() const noexcept
std::shared_ptr< Image > ImagePtr
var source = new CancellationTokenSource();
var token = source.Token();
var image = stream.Wait(out status, token)
source.Cancel();
token.IsCanceled()
import cvb
token = source.token
image, status, node_maps = stream.wait(token)
source.cancel()
token.is_canceled()
Note: Error handling has been omitted from the above example.
- Create a cancellation token source object, which provides tokens and the signal to cancel.
- Get a cancellation token.
- Pass the cancellation token to the wait function.
- Cancel the wait function by using the cancellation token.
- Returns, whether or not the cancellation token has been canceled.