February 14th, 2011, 01:06 PM
WebClient.CancelAsync() is funky.
.NET 3.5 (WebClient from .NET 1.1)
I'm using a WebClient class to check a file on an FTP server at specified intervals using a timer. I basically call WebClient.DownloadStringAsync() and wait for the WebClient.DownloadStringAsyncCompleted event to be fired and then do some processing with the result. However, the call to DownloadStringAsync() blocks the calling thread for a period of time and if you call WebClient.CancelAsync() during that time from another thread, it causes some problems. It will not actually cancel the async operation and the WebClient will never change from a busy state (IsBusy property will remain true indefinitely) and the DownloadStringAsyncCompleted event will never get raised.
This is a rather noxious problem that I'm not sure how to get around. I could try implementing a WebRequest class, but the ease of using the WebClient class is tempting and I want to see if I can find a fix for it first. So far though, I don't think there is one without calling WebClient.Dispose() after the WebClient.CancelAsync() call which may result in some undefined behavior.
Last edited by RaleTheBlade; February 14th, 2011 at 01:08 PM.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.