DownloadStringAsync () does not load the string asynchronously

An attempt to implement downloadStringAsync()to prevent the user interface from freezing for 10 seconds when loading one byte of data. However, although the download is complete, it freezes the user interface just as if I were using it downloadString().

Here is my code:

    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

        }
    }
+3
source share
1 answer

The same problem was found and a solution was found. Here's a pretty tricky discussion: http://social.msdn.microsoft.com/Forums/en-US/a00dba00-5432-450b-9904-9d343c11888d/webclient-downloadstringasync-freeze-my-ui?forum=ncl

, , - - . :

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

All Articles