为什么DownloadStringAsync()阻塞线程? [英] Why is DownloadStringAsync() Blocking the Thread?

查看:142
本文介绍了为什么DownloadStringAsync()阻塞线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的应用程序中有一个WebClient.当我在WebClient对象中调用DownloadStringAsync函数时,它将锁定我的应用程序,直到完成下载.为什么?

Ok, I have a WebClient in my application. When I call DownloadStringAsync function in the WebClient object, it locks up my application until it finishes the download. Why?

推荐答案

此方法不应阻止您的线程.如果可能的话,其他东西会阻塞.是否通过其他一些阻塞调用和一些线程同步原语将线程置于等待状态?

问题不在这里.我认为仅使用利用线程的阻塞调用要容易得多.为您的网络创建一个单独的线程(取决于应用程序的角色和您可能需要创建一个以上线程的需求,服务器端通常需要更多线程,尽管要避免使用可变数量的线程,但要创建固定的预定义数量的线程).

—SA
This method should not block your thread. Probably something else blocks if. Do you put the thread in the wait state by some other blocking call, some thread synchronization primitives?

The problem is not here. I think it''s much easier to use only the blocking calls utilizing threads. Create a separate thread for your networking (depending on the role of your application and requirements you may need to create more then one, server side usually needs more threads, avoid using variable number of threads though, create a fixed predefined number of them).

—SA


在IE中,您是否启用了自动代理检测?如果是这样,请尝试禁用它或手动配置代理,然后看看会发生什么.

如果那不起作用,请禁用所有防火墙,然后重试.
In IE, do you have Automatic Proxy Detection enabled?? If so, try disabling it or configuring the proxy manually and see what happens.

If that doesn''t work, disable any firewall you have and try it again.


否.如果客户端启用了相同的检测代理设置",则在进行检测时它将阻塞约15秒钟.

您可以尝试将WebClient.Proxy属性设置为 GlobalProxySelection.GetEmptyWebProxy返回的值 [ ^ ],它将忽略所有代理设置并停止检测.

这样做的问题是,如果用户依赖于自动检测,则显然您不能这样做,而不得不忍受该块.
No. If the client has the same "detect proxy settings" enabled, it''ll block for about 15 seconds while the detection is going on.

You can try to set the WebClient.Proxy property to whatever is returned by GlobalProxySelection.GetEmptyWebProxy[^], which will ignore all proxy settings and stop the detections.

The problem with doing this is if the user depends on the automatic detection, you obviously can''t do this and have to put up with the block.


这篇关于为什么DownloadStringAsync()阻塞线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆