开发环境:
C#、视觉工作室 2010 (.net 4.0)、win7 x64
winform项目中的代码:
private void Form1_Load(object sender, EventArgs e)
{
string path = "c:\\1.jpg";
for (int i = 0; i < 10; i++)
{
string url = "http://...." + i.ToString() + ".jpg";//i'm sure the http file does exist
using (WebClient wc = new WebClient())
{
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(new Uri(url), path);
Thread.Sleep(3000);//i'm sure the download will be finished in 3s
WriteLog("C:\\1.log", "main function\r\n");
}
}
}
static void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
WriteLog("C:\\1.log", "callback function\r\n");
}
static void WriteLog(string LogName, string log)
{
StreamWriter sw = new StreamWriter(LogName, true);
if (sw == null)
return;
sw.Write(System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + log);
sw.Close();
}
那么日志将是:
2017/11/03 19:04:48 主要功能
2017/11/03 19:04:51 主要功能
2017/11/03 19:04:54 主要功能
2017/11/03 19:04:57 主要功能
2017/11/03 19:05:00 主要功能
2017/11/03 19:05:03 主要功能
2017/11/03 19:05:06 主要功能
2017/11/03 19:05:09 主要功能
2017/11/03 19:05:12 主要功能
2017/11/03 19:05:15 主要功能
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
2017/11/03 19:05:15 回调函数
如果 ConsoleApplication 中的代码相同:
static void Main(string[] args)
{
string path = "c:\\1.jpg";
for (int i = 0; i < 10; i++)
{
string url = "http://...." + i.ToString() + ".jpg";//i'm sure the http file does exist
using (WebClient wc= new WebClient())
{
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(new Uri(url), path);
Thread.Sleep(3000);//i'm sure the download will be finished in 3s
WriteLog("C:\\1.log", "main function\r\n");
}
}
}
static void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
WriteLog("C:\\1.log", "callback function\r\n");
}
static void WriteLog(string LogName, string log)
{
StreamWriter sw = new StreamWriter(LogName, true);
if (sw == null)
return;
sw.Write(System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + log);
sw.Close();
}
那么日志将是:
2017/11/03 19:13:50 回调函数
2017/11/03 19:13:52 主要功能
2017/11/03 19:13:53 回调函数
2017/11/03 19:13:55 主要功能
2017/11/03 19:13:56 回调函数
2017/11/03 19:13:58 主要功能
2017/11/03 19:13:59 回调函数
2017/11/03 19:14:01 主要功能
2017/11/03 19:14:02 回调函数
2017/11/03 19:14:04 主要功能
2017/11/03 19:14:05 回调函数
2017/11/03 19:14:08 主要功能
2017/11/03 19:14:08 回调函数
2017/11/03 19:14:11 主要功能
2017/11/03 19:14:11 回调函数
2017/11/03 19:14:14 主要功能
2017/11/03 19:14:14 回调函数
2017/11/03 19:14:17 主要功能
2017/11/03 19:14:17 回调函数
2017/11/03 19:14:20 主要功能
显然,第二个结果是对的。
但是在第一个项目中,为什么在所有下载完成之前不调用 DownloadFileCompleted 事件?
每次下载完成后如何立即调用 DownloadFileCompleted 事件?