using DownloadFileCompleted

So you wire up an event when using WebClient.DownloadFileCompleted. But how do you know which file completed? you may be using a loop to download a bunch of files and to do something specific when each one finishes. The event fired looks like this:
static void myEvent_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
both e and sender don’t seem to contain even the name of the file downloaded. What did was, add to the WebClient object’s QueryString property a value which I then look at. See an example below:

static void Main(string[] args)

{

WebClient x = new WebClient();

x.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(myEvent_DownloadFileCompleted);

x.QueryString.Add(“what”, “x”);

x.DownloadFileAsync(new Uri(“http://www.google.com/intl/en_ALL/images/logo.gif”), “c:\\x.gif”);

WebClient y = new WebClient();

y.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(myEvent_DownloadFileCompleted);

y.QueryString.Add(“what”, “y”);

y.DownloadFileAsync(newUri(“http://www.google.com/intl/en_ALL/images/logo.gif”), “c:\\a.gif”);

}

static void myEvent_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)

{

Console.WriteLine(((System.Net.WebClient)(sender)).QueryString["what"]);

}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.