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

北慕城南 提交于 2019-12-08 04:48:23

问题


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:


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 + "%)"
            );
    }
}


来源:https://stackoverflow.com/questions/27261797/webclient-downloadprogresschanged-console-writeline-is-blocking-ui-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!