WebClient.DownloadProgressChanged: Console.WriteLine() is blocking UI thread

1.6k views Asked by At

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.

1

There are 1 answers

1
Grant Winney On BEST ANSWER

The way you're doing it seems to be how MSDN shows in its examples. I tried it too and got the same result. You'll see similar behavior when running something in a separate thread, which then calls back to the main UI thread too quickly and pounds it with updates. The UI thread gets backed up and effectively freezes.

That DownloadProgressChanged event fires really quickly... appears to be hundreds of times per second, meaning it's trying to write to the console that quickly too.

You can limit how often you're writing to the console, which will resolve the issue (I tested it by trying to download a 4GB ISO, and it wrote to the console while leaving the UI responsive):

// define a class-level field variable
private int counter;

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    counter++;

    // Only print to the console once every 500 times the event fires,
    //  which was about once per second when I tested it
    if (counter % 500 == 0)
    {
        //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 + "%)"
            );
    }
}