Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

windows

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?

like image 357
Mark S Avatar asked Mar 06 '16 19:03

Mark S


People also ask

What is '~' in C language?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What does -= mean in C++?

-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C - A.


1 Answers

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.

like image 68
Matti Virkkunen Avatar answered Oct 31 '22 06:10

Matti Virkkunen