TEst16
TEst16

Reputation: 407

Run a task in background and continue execution

I have a long running library method (3rd party) that returns a System.Threading.Tasks.Task. How can I tell it to run in background and continue my code execution? I'd also like to get a callback once it finishes successfully or with an exception.

Upvotes: 0

Views: 437

Answers (1)

Joe White
Joe White

Reputation: 97656

If it returns a Task, then it almost certainly does run in the background. An instance of Task represents the completion status of something that's running asynchronously; that's what the Task class is for.

And the Task gives you the opportunity to request a callback. Just call .ContinueWith(your callback).

There are several overloads of ContinueWith that let you specify options if you wish, like "only call my callback if the task completed successfully" or "only call my callback if the task failed with an exception".

Upvotes: 3

Related Questions