本文介绍了反应迟钝,在.NET 4.5中WPF异步事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了被踢单击该按钮时,一个简单的异步操作。这里是整个code:

 公共部分类主窗口:窗口{    公共主窗口(){
        的InitializeComponent();
    }    私人异步无效Button_Click_1(对象发件人,RoutedEventArgs E){        VAR htmlString =等待DowloadPage(http://example.com);
        txtBlock1.Text = htmlString;
    }    公共异步任务<串GT; DowloadPage(字符串URI){        使用(Web客户端的客户端=新的WebClient()){            VAR htmlString =等待client.DownloadStringTaskAsync(URI);
            返回htmlString;
        }
    }}

很容易。但是,当我按一下按钮,我体验到UI线程上反应迟钝。当我尝试左右,而该页面正在下载的窗口移动,我无法。

任何想法是怎么回事了?

编辑:

我试着用HttpClient的在.NET 4.5和它的工作了pretty巨大的预期:

 公共异步任务<串GT; DowloadPage(字符串URI){    使用(HttpClient的客户端=新的HttpClient()){        变种响应=等待client.GetAsync(URI);
        VAR htmlString =等待response.Content.ReadAsStringAsync();
        返回htmlString;
    }
}


解决方案

Web客户端使用的HttpWebRequest ,不幸的是是不是很异步,即使您使用了异步的方法。它阻挡DNS查找,至少。它也可能代理协商和/或初始HTTP连接时阻止

的HttpClient 的较旧版本只是周围使用的HttpWebRequest 的包装。我要求一个真正的异步的HttpClient ,但从来没有听说过的响应。我最后一次检查的HttpClient ,它仍然是MVC的一部分;的ASP.NET Web API不在身边那个时候,所以他们可能有固定的的HttpClient 至今。的或者的你的机器上 Web客户端的HttpClient 之间的行为差​​异可能只需要做与DNS缓存或一些这样的。

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;   
    }
}

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.

这篇关于反应迟钝,在.NET 4.5中WPF异步事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:23