user823871
user823871

Reputation:

how to cancel a Task when the CancellationTokenSource is not available?

I have a function, which returns a Task to do anything with that.

I have a custom form, ProgressDialog, whose constructor takes a Task to execute. This dialog displays the state of the Task, but it's not important.

The dialog has a Cancel button. If the user clicks on that, the Task should be canceled, but how can I do this? There I have only the Task object, not the CancellationTokenSource (which I could cancel the Task with).

Why can't I cancel the Task via the Task object itself?

Upvotes: 0

Views: 2311

Answers (1)

bobbymcr
bobbymcr

Reputation: 24177

The short answer would be you can't do this. If you want to cancel something, you need to provide a cancellation token of some form. The expected pattern is outlined nicely here:

Upvotes: 3

Related Questions