I have a Task returned from a third party. It does not give me the option to insert a cancellation token when constructing the task. Can I insert a cancellation token into a task after the fact?
Something like this:
Task thirdPartyTask = ThirdParty.GetTask();
CancellationTokenSource cts = new CancellationTokenSource();
thirdPartyTask.SetCancellationToken(cts.Token);
var result = await thirdPartyTask;
cts.Cancel();
I am aware the normal way is to insert the cancellation token during construction, but I do not have access to that, since it is third party.
I am forced to use the await
keyword, because I am in Unity.
I want to cancel the task in a non-cooperative fashion. I want to abort/kill the task against its will.
How can I cancel a task that I didn't create?