GetJsonAsync starts the REST request by calling HttpClient.GetStringAsync (still within the context). The top-level method calls GetJsonAsync (within the UI/ASP.NET context). So this is what happens, starting with the top-level method ( Button1_Click for UI / M圜ontroller.Get for ASP.NET): Take a look at this example, Stephen has a clear answer for you: I'm completely clueless about what the problem is. ![]() Why doesn't the main thread continue it's continuation logic and returns a string result from GetDataAsync() ?Ĭan someone please explain me why there is a deadlock in the above example? If I try to dissect it myself, the main thread spawns to a new one in MyWebService.GetDataAsync(), but since the main thread awaits there, it waits on the result in GetDataAsync().Result. Var result = await MyWebService.GetDataAsync() DEADLOCK: this blocks on the async task ![]() If you spawn off a task from a single-threaded context, then wait for that task in the context, your waiting code may be blocking the background task. In these single-threaded synchronization contexts, it’s easy to deadlock yourself. An example of this is the Windows UI thread or the ASP.NET request context. This means only one unit of work can be executed in the context at a given time. Some synchronization contexts are non-reentrant and single-threaded. Stability: Know your synchronization contexts One of the advices given was the following: ![]() I came across some best practices for asynchronous programming using c#'s async/ await keywords (I'm new to c# 5.0).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |