0

For downloading a number of images I'm making DownloadDataAsync calls to separate instances of WebClient, in a loop, with a thread sleep delay. I expected the response to happen on separate threads, but it seems not, as response only occurs after all calls are completed.

So what's an appropritate fix for this? Is there an alternative client type, or should I make a thread for each webclient call?

So currently I'm calling this in a loop:

 Private Sub StartDownload(ByVal webImageLink As String, ByVal token As Object)
    Dim wc As New WebClient
    Try
        AddHandler wc.DownloadDataCompleted, AddressOf OnDownloadCompleted

        wc.DownloadDataAsync(New System.Uri(webImageLink), token)

        Threading.Thread.Sleep(delay)

    Catch ex As Exception
        Debug.Print("Exception in ImageDownloader.DoDownload ")
    End Try
End Sub
1
  • @Strillo, done, I assume that's all that's relevant.. Commented Mar 27, 2012 at 12:15

1 Answer 1

1

Feature, not a bug. WebClient makes an effort to raise the DownloadCompleted event on the same thread, if it can. It can when you call it from the UI thread of a Winforms or WPF application. Which is usually desirable, you can update the UI in the completion event handler without having to use Control/Dispatcher.BeginInvoke(). But with the side-effect that this won't happen until your code stops running so the event handler can be called. It is dispatched by the UI thread's dispatch loop.

A workaround, if you really need one, is to start the downloads with a little helper method that you start with ThreadPool.QueueUserWorkItem() or a Thread.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Now using ThreadPool. May I just ask, if I didn't, and downloaded a few hundred MB of pictures, from say 5-10000 calls, where would this memory "pile up", and would that be considered a poor practice? (Not considering UI freeze)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.