Skip to content

Access to token when the Task action is defined in another scope #763

@TPucke

Description

@TPucke

I'm using the overload Task.Run(Action, CancellationToken). The example code shows how to handle the cancellation, but the token variable inside the anonymous action appears to be available because it is declared in the outer scope. How is a cancellation token accessed within the action body when the action is defined in another scope, such as in another class. I have made this work by passing the token both as second argument to Task.Run and as an argument to my action method, but surely it is not intended that this token should have to be passed twice (redundantly). Since the token is passed to Task.Run it must be available inside the action by some local reference.

Enhancing the example code with this more general usage would be helpful.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions