I am trying to implement an Asynchronous file download from a web server using
private void btnDownload_Click(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("test", "test");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("ftp://2.1.1.1:17865/zaz.txt"), @"c:\myfile.txt");
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
Now the download works fine, what I would like to know is the following:
assuming that I will have more than 1 download at a time, what is the best way to keep track of each download and report progress separately ?
I was looking for a tag property of the WebClient, but could not find one.
Thank you
You can pass a third argument to DownloadFileAsync (
userToken). It can be anything you want, and you can retrieve it in theDownloadProgressChangedevent arguments, (UserStateproperty). So you just need to pass the URI or some unique ID as the user token, so that you can identify the download for which the progress has changed