Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Difference Between Awaiter (GetAwaiter) and ContinueWith

In .net 4.0, I use Task.ContinueWith regularly. But then I spotted "task.GetAwaiter()" which seems to have the same purpose.

What is the difference?

like image 288
Brent Arias Avatar asked Apr 30 '13 18:04

Brent Arias


People also ask

What is ContinueWith?

The ContinueWith function is a method available on the task that allows executing code after the task has finished execution. In simple words it allows continuation. Things to note here is that ContinueWith also returns one Task. That means you can attach ContinueWith one task returned by this method.

What is a GetAwaiter?

GetAwaiter() method, which returns an instance that has a GetResult() method. When used on a faulted Task, GetResult() will propagate the original exception (this is how “ await task; ” gets its behavior). You can thus use “ task.

What is Awaitable in C#?

The await keyword in C# programming language is used to suspend all async methods enclosed until the point where the operations presented by the asynchronous method are completed. In order or a developer to call multiple functions in an asynchronous way, async and await are highly used and recommended.


1 Answers

If you're targeting .NET 4, you'd use ContinueWith.

In general, you wouldn't normally use task.GetAwaiter(). This method exists in order to support the await keyword, and is not part of .NET 4 (it's added in 4.5). This isn't something you'd typically use directly yourself, but instead write it as part of an async method.

like image 54
Reed Copsey Avatar answered Oct 11 '22 08:10

Reed Copsey