e4rthdog
e4rthdog

Reputation: 5223

Async / await with task.run vs task.run and continuewith in .NET 4.0

I have installed async package in .net 4.0. this gives me the ability to use async / await keywords in my applications.

as i have understood until now i can use wrap my task.run code in async / await and have the same result as using task.run with continuewith.

Is this true? or there are deeper differences?

Upvotes: 9

Views: 5429

Answers (2)

AlexH
AlexH

Reputation: 2700

There will be a difference if you introduce async keyword in your functions prototypes; the exceptions will be thrown at the Task caller's level. Without the async keyword, you have to check the TaskContinuationOptions.OnlyOnFaulted status to get the exception.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500065

It depends on what you're doing with ContinueWith. But yes, often you can use await to achieve the same effects you'd previously have achieved using ContinueWith. What you can't do is things like "continue with this code only on failure" - you just use normal exception handling for that. As AlexH says, there will be further differences in terms of the overall behaviour of your async method - but in most cases I'd say that's desirable. Basically, the asynchrony of the code flows, so async methods tend to call more async methods, etc.

I suggest you read up on what async/await is about (there are loads of resources out there - I'd recommend the "Consuming the Task-based Asynchronous Pattern" page on MSDN as one starting point.

Upvotes: 11

Related Questions