Recently I have been working with the C# asynchronous computing feature exposed by the async
method modifier, and the await
keyword for tasks that I need the result of. I have used this feature in the past a lot, but the concept that I am still having a very hard time getting a grasp on is deadlocking. I have read numerous explanations; however, I still cannot tell, just by looking, whether or not a method will deadlock, or if I can validly make another call to an asynchronous method without accidentally deadlocking if the Task
returned by the method is not used correctly.
I do not really understand what using the await
keyword does that's so special, so as to cause a result to never be returned if a user tries to get the Result
property, for example, of a Task
returned from an asynchronous method.
Also what are the cases where it would be allowed? I know there is something about blocking and non-blocking operations, but I mean generally what code will cause this issue and what code won't?
Before anyone says that it's due to the fact that getting Result
blocks the thread until the Task
finishes executing causing it to be impossible for the result to be populated because it was running on the same thread, I know! I just don't understand; when you don't use any sort of asynchronous pattern, doesn't the result from a method need to be waited for anyways?