The following code doesn’t work, somehow I can’t get the int value from the ‘Completed’ method to my btn_Start_Click method:
private void btn_Start_Click(object sender, EventArgs e)
{
int completedDownload = 0;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri("http://somesite.com/file.jpg"), @"c:\file.jpg");
if (Completed.completeDownload == 1)
{
//open the file code goes here.
}
//Rest of the code goes here.
//and here
//and here
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
completedDownload = 1;
}
From the remarks on the
WebClient.DownloadFileAsyncfunction:MSDN Documentation
It seems like a better choice to fire a function when the file is completed would involve using an event handler. Here is an example using the
DownloadFileCompletedhandler:MSDN Documentation