Monday, 7 November 2016

c# - Using async function in sync function

I have an httpClient with async() methods, I need to use in sync methods called from the GUI thread. I cannot use the .Result property of the task because it causes a deadlock and freezes the GUI.



I have a solution with launching a new thread pool thread and executing an async method in it:




Task.Run(async () => { ApiInfo = await _apiClient.Get($"{Url}"); }).Wait();


This works, but because I'm new to async programming - I'm not sure if it's the preferred way for my situation?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...