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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With