Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .NET Task.Result block(synchronously) a thread [duplicate]

Does Task.Result synchronously block current thread such that it cannot be used to perform other operations while it is waiting for the task complete?

For example, if I call Task.Result in a ASP.NET execution path, the current thread cannot be used to process other requests while it's awaiting the result.

By the way, how about the async in C# 5.0? Does await async method block the current thread?

Thanks for any comment.

like image 913
Ricky Avatar asked Dec 26 '16 06:12

Ricky


People also ask

Does Task result block thread?

Yes Accessing Task. Result is the same as calling Task. Wait(). Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method.

What is difference between Task and thread in C#?

Differences Between Task And ThreadThe Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel. The task can return a result.

What is the data type of the result property of the Task class?

The Task<T> class has a Result property of type T that contains whatever you pass back with return statements in the method. The caller generally retrieves what's stored in the Result property implicitly, through the use of await .

What is the difference between await and result?

Result() it blocks the thread until a result is returned before continuing to the next line of code. When you use await you are also awaiting an synchronous call to complete before the code following t (continuation step).


1 Answers

MSDN: https://msdn.microsoft.com/en-us/library/dd321468(v=vs.110).aspx

Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method

MSDN: https://msdn.microsoft.com/en-us/library/hh156528.aspx

An await expression does not block the thread on which it is executing

like image 120
Lanorkin Avatar answered Oct 23 '22 15:10

Lanorkin