I have the following simple code:
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 + "%)"
);
}
Why is this blocking the UI thread? When I replace the Console.WriteLine()
with code to update my progress bar (not show in code), it works. The UI is responsive.