我有以下简单的代码:
private void btn_download_Click(object sender, EventArgs e){
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileAsync(new Uri("http://.../file.zip"), "file.zip");
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e){
//Prints: "Downloaded 3mb of 61.46mb (4%)"
Console.WriteLine("Downloaded "
+ ((e.BytesReceived / 1024f) / 1024f).ToString("#0.##") + "mb"
+ " of "
+ ((e.TotalBytesToReceive / 1024f) / 1024f).ToString("#0.##") + "mb"
+ " (" + e.ProgressPercentage + "%)"
);
}
为什么这会阻塞 UI 线程?当我Console.WriteLine()
用代码替换以更新我的进度条(未显示在代码中)时,它可以工作。UI 是响应式的。