C# – WebClient does not support concurrent I/O operations.

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. 

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

Solution to answer:

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.