Unresponsiveness with async event handlers in WPF

2019-04-13 07:16发布

问题:

I have created a simple async operation which is being kicked of when the button is clicked. Here is the whole code:

public partial class MainWindow : Window {

    public MainWindow() {
        InitializeComponent();
    }

    private async void Button_Click_1(object sender, RoutedEventArgs e) {

        var htmlString = await DowloadPage("http://example.com");
        txtBlock1.Text = htmlString;
    }

    public async Task<string> DowloadPage(string uri) {

        using (WebClient client = new WebClient()) {

            var htmlString = await client.DownloadStringTaskAsync(uri);
            return htmlString;   
        }
    }

}

Very easy. But when I click the button, I experience unresponsiveness on the UI thread. When I try to move around the window while the page is being downloaded, I am unable to.

Any idea what is going wrong?

Edit:

I tried with HttpClient in .NET 4.5 and it worked out pretty great as expected:

public async Task<string> DowloadPage(string uri) {

    using (HttpClient client = new HttpClient()) {

        var response = await client.GetAsync(uri);
        var htmlString = await response.Content.ReadAsStringAsync();
        return htmlString;   
    }
}

回答1:

WebClient uses HttpWebRequest, which unfortunately is not very asynchronous, even if you use the "asynchronous" methods. It does a blocking DNS lookup, at least. It may also block during proxy negotiation and/or the initial HTTP connection.

An older release of HttpClient was just using a wrapper around HttpWebRequest. I requested a truly-asynchronous HttpClient, but never heard a response. The last time I checked HttpClient, it was still part of MVC; ASP.NET Web API wasn't around at that time, so they may have fixed HttpClient since then. Or the difference in behavior between WebClient and HttpClient on your machine may just have to do with DNS caches or some such.