webclients事件“DownloadFileCompleted”没有开火! [英] webclients event "DownloadFileCompleted" isnot firing !

查看:233
本文介绍了webclients事件“DownloadFileCompleted”没有开火!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using(WebClient webClient = new WebClient())
             {
             MessageBox.Show("inside webclient***");
             webClient.DownloadFile(file, temp_cmp);  // this statement will download the content url/file into the filecreated temp_cmp
             webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(Completed);
             webClient.DownloadDataCompleted +=new DownloadDataCompletedEventHandler(Completed);
             webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
             MessageBox.Show("after webclient.downloadfile()");
             Thread.Sleep(1000);
             }





以下是我的事件处理程序。我想尝试所有可能性,这就是为什么,我写了很多可能性





and following are my event handlers. i wana try all possibilities, thats why, i wrote many of them

 void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
      {
 MessageBox.Show("Download Completed");
 throw new NotImplementedException();
  }
void Completed(object sender, AsyncCompletedEventArgs e)
  {
 MessageBox.Show("Download Completed");
 MessageBox.Show(e.ToString());
  }

推荐答案

事件附件写在DownloadFile之后。



你可以使用:



Events attachment is written after the "DownloadFile".

you may use:

private void btnDownload_Click(object sender, EventArgs e)
{
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt");
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
  MessageBox.Show("Download completed!");
}


使用webclient的方法检查行的顺序。



1你正在使用DownloadFile,但是为异步提供代码DownloadFileAsync(通常下载功能是异步的)。

2.你在开始时没有附加事件就会触发DownloadFile这个方法。



首先附上事件而不是点燃它
check the order of line with method of webclient.

1. you are using "DownloadFile", but in provide code "DownloadFileAsync" for async (usually downloading function are async).
2. you have fire "DownloadFile" this method at start with out attaching the events.

first attach the events than fire it


这篇关于webclients事件“DownloadFileCompleted”没有开火!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆