views:

472

answers:

2

Hi everyone,

I have an array of file names which I want to download.

The array is currently contained in a string[] and it is working inside of a BackgroundWorker.

What I want to do is use that array to download files and output the result into a progress bar which will tell me how long I have left for completion.

Is there a way I can do this.

Thanks.

A: 

Simply use: ReportProgress((100 * currentItem) / list.Count) and let the BackgroundWorker.ProgressChanged event handler update the progress bar (you might have to check if invoke is required and use invoke to update).

The ProgressChanged event handler can then calculate the remaining time.

Ofc this is not precise if the size of the elements varies much.

You might want to look at http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadfileasync.aspx which removes the need for a seperate background worker.

dbemerlin
A: 

Use WebClient to download files, there's a method called "DownloadFile".

Pseudo code

foreach(var url in listOfStringURLS)
{
    WebClient.DownloadFile(url, ".");
    updateProgressBar();
}

That's the Easy part! Now, you need to know that when you are using a BackgroundWorker, you are no longer on the GUI-thread, which means that you Cannot update the ProgressBar without using a Delegate! Check this out: "Progress Bar Delegate".

For .NET 4.0

You might want to parallelize the downloads! You can do this easily with .NET 4.0 by looking in to Tasks, check this blog about Paralell Programming. Otherwise you will, as another poster said have all IO on 1 thread, which may be not so optimized.

Filip Ekberg