Mark S Mark S - 1 year ago 161
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download