c# - DownloadStringAsync() 不异步下载字符串

标签 c# asynchronous webclient downloadstring

尝试实现 downloadStringAsync() 以防止在下载 一个字节 数据时 UI 卡住 10 秒。但是,即使下载完成,它也会卡住 UI,就像我使用 downloadString() 一样。

这是我的代码:

    public void loadHTML()
    {
            WebClient client = new WebClient();

            // Specify that the DownloadStringCallback2 method gets called
            // when the download completes.
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(loadHTMLCallback);
            client.DownloadStringAsync(new Uri("http://www.example.com"));
            return;
    }

    public void loadHTMLCallback(Object sender, DownloadStringCompletedEventArgs e)
    {
        // If the request was not canceled and did not throw
        // an exception, display the resource.
        if (!e.Cancelled && e.Error == null)
        {
            string result = (string)e.Result;

            // Do cool stuff with result

        }
    }

最佳答案

遇到了同样的问题,找到了解决办法。 这里的讨论相当复杂: http://social.msdn.microsoft.com/Forums/en-US/a00dba00-5432-450b-9904-9d343c11888d/webclient-downloadstringasync-freeze-my-ui?forum=ncl

简而言之,问题是 Web 客户端正在搜索代理服务器并挂起应用程序。 以下解决方案有帮助:

WebClient webClient = new WebClient();
webClient.Proxy = null;
... Do whatever else ...

关于c# - DownloadStringAsync() 不异步下载字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6119030/

相关文章:

c# - 通用类型列表

c# - 如何将 C# 类映射到 Angular 对象

javascript - 使用 JQuery Promises 传递参数

c# - wb.DownloadFileAsync 抛出 "WebClient does not support concurrent I/O operations."异常

c# - 单击按钮时弹出窗口关闭并重定向到不同页面!

C++ 异步任务。包含 "Future"麻烦

java - 在 Java 中使异步 Web 服务调用成为同步调用

c# - 如何使用c#从谷歌驱动器下载文件

c# - 从 wcf c# 中的 URL 下载 zip 文件

c# - 帕斯卡三角函数