本文介绍了在C#中,HttpClient.getStringAsync()方法的同步替代项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在winform中,单击按钮会调用此方法以将链接内容下载为字符串,然后在文本框中显示字符串长度.这一切都是异步发生的.有办法同步执行此操作吗?

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?

推荐答案

您只需采用 .Result :

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

但是,如果它在UI线程上运行,则不应这样做.在UI线程上阻塞不是很好.拥抱异步性.

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.

这篇关于在C#中,HttpClient.getStringAsync()方法的同步替代项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:16