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

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

问题描述

如果用户单击某个按钮,我将使用 WebClient 的 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. 

有没有办法强制 WebClient 取消其当前操作并开始新的操作?

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

推荐答案

这似乎是您尝试在第一次下载完成之前下载第二个文件 - 使用相同的 WebClient 实例.创建 WebClient 的新实例肯定可以解决这个问题.CancelAsync() 尝试取消正在进行的下载,但它可能仍在运行一小段时间.下载完成/取消后,将引发 DownloadStringCompleted 事件处理程序,允许您使用相同的 WebClient 实例开始新的下载.

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.

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

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