Mark S Mark S - 22 days ago 11
C# Question

In C#, what is a synchronous alternate for HttpClient.getStringAsync() method?

In a winform, clicking on a button calls this method to download link content as string and then displays string length in a textbox. This all happens asynchronously. Is there a way to do this synchronously?

Answer

You can make any task block by just taking the .Result:

string response = client.GetStringAsync(...).Result;

However if this is running on a UI thread you should not do that. Blocking on the UI thread is not nice. Embrace the asynchronicity.

Comments