Web客户端不支持并发I / O操作。 [英] WebClient does not support concurrent I/O operations.

查看:359
本文介绍了Web客户端不支持并发I / O操作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Web客户端的DownloadStringAsync方法,如果用户点击一个特定的按钮,下载一个HTML页面。如果他们想它完成之前,停止当前操作,并开始一个新的,我呼吁CancelAsync方法,还可以设置WebClient的对象为null。在第二个按钮的事件处理程序,我也重新初始化WebClient的对象,最后尝试下载新的内容。 WebClient的对象是一个全局变量。我得到的错误是:

I'm using WebClient's DownloadStringAsync method to download an html page if the user clicks a certain button. If they want to stop the current operation before it's completed and start a new one, I call CancelAsync method and also set the WebClient object to null. In the event handler of the second button, I also reinitialize the WebClient object and finally attempt to download new content. The WebClient object is a global variable. The error I'm getting is:

WebClient does not support concurrent I/O operations. 



有没有办法迫使Web客户端取消其当前操作,并开始一个新的?

Is there a way to force the WebClient to cancel its current operation and start a new one?

推荐答案

这好像你试图下载第二个文件先下载完成之前 - 与Web客户端的同一个实例。创建Web客户端的新实例,一定要解决这个问题。 CancelAsync()尝试取消正在进行的下载,但它可能一小会儿来仍在运行。完成下载/取消后,DownloadStringCompleted事件处理程序将得到提升,让您开始与Web客户端的同一个实例新的下载。

This seems like you are trying to download the second file before the first download was completed - with the same instance of WebClient. Creating new instance of WebClient should definitely solve that issue. CancelAsync() attempts to cancel the download in progress, but it might be still running for a short while. After the download is completed/canceled, the DownloadStringCompleted event handler will be raised, allowing you to commence new download with the same instance of WebClient.

这篇关于Web客户端不支持并发I / O操作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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