Wednesday, March 8, 2017

CancellationToken in C#

Per this "A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects." and you kinda make one like this:

CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;

 
 

I see these handed in at method signatures that return a Task of T from which the result will get assigned to a variable while using the await keyword like so:

var myResult = (await myMethod(myRealVariable, token));

 
 

Mysteriously there is little more than that. You don't really have to do anything with the token inside the method. I don't immediately understand how it helps and what the pain points are of not having it.

No comments:

Post a Comment