2

I'm working on a WPF download manager which should support downloading multiple files at the same time through HTTP, adding a new download, pause/resume, displaying and updating each download process information (filename, size, download percentage, time left...) in a DataGrid, displaying progress in a ProgressBar and a bunch of other stuff.

So, the idea is to use a separate thread for each download process, and be able to dynamically create/cancel threads. What is the best way to accomplish this? Using a BackgroundWorker, ThreadPool?

marko
  • 217
  • 1
  • 4
  • 14

2 Answers2

6

The WebClient XXXAsync methods such as DownloadFileAsync already provide you with asynchronous versions of all methods that allow you to perform the HTTP requests on separate threads. They are also C# 5.0 async ready.

Since this is a WPF application you should ensure that you are modifying the UI controls only on the main UI thread using the Dispatcher.BeginInvoke method.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yes, well I cannot use WebClient, since it doesn't support pausing/resuming a download, so I have to create a custom wrapper for HttpWebRequest. I think I will have to use a new `Thread` for each process and set it as a background thread. – marko Feb 05 '12 at 22:44
0

I would just use Thread directly and keep the references to all the treads that I create.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126