Multi-File Download with C# WebClient...
Posted on 2011-02-17
Can the WebClient class successfully, in a multi-threaded application, download multiple files?
I have a multi-threaded application that allows the user to download up to 5 files at a time.
To accomplish this I run the download in 2 phases: Phase 1 calls DownloadFileAsync(uri,downloadpath,object) 5 times (I use AutoResetEvent objects for thread synchronization). Phase 1 runs with no problems.
Phase 2 is the handler for the AsyncCompletedEvent and where the problem occurs. When the program enters the download complete handle I am finding the file information contained in the response header for the sender object (ResponseHeaders["Content-Disposition"]), is different than the file in the AsyncCompletedEventHandler.UserState!
What I expected was completed events to be fired for each download request, but it looks like the completed event is using the first or the last request as the sender and eventually the sender value changes to null. As noted earlier, the sender never matches the event arg UserState.
If I download 1 file at a time, no problems - the response header content matches the UserState object.
Any ideas on what I am doing wrong would be greatly appreciated.
Thanks in advance.