为什么DownloadStringAsync()阻塞线程?

编程入门 行业动态 更新时间:2024-10-28 21:18:50
本文介绍了为什么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()阻塞线程?

本文发布于:2023-10-09 02:03:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   DownloadStringAsync

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!