Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone BackgroundWorker for WebClient?

Now the WebClient issue is fixed and can return on a background thread i'd like to start using it in this way.

After much searching I've come up with this code that appears to work fine, is this all there is to it?

BackgroundWorker bw = new BackgroundWorker();

bw.DoWork += (s,e) =>
{
    WebClient wc = new WebClient();

    wc.DownloadStringCompleted += DownloadStringCompleted;
    wc.DownloadStringAsync(url);
};

bw.RunWorkerAsync();

In DownloadStringCompleted I send the result back to the UI thread.

Have I missed anything important or is it really this simple?

like image 739
Jim Avatar asked Feb 24 '23 00:02

Jim


2 Answers

I don't get why you would want to run the WebClient on a background thread in the first place, since the WebClient is already creating a thread for the downloading part.

The difference is that the WebClient is running it's DownloadStringCompleted event on the UI thread. Which it still would do in your code.

I would suggest you use WebRequest class instead. The use of the WebRequest class can be greatly simplified with a simple extension method, that makes it behave like the WebClient.

public static class WebRequestEx
{
    public static void DownloadStringAsync(this WebRequest request, Action<string> callback)
    {
        if (request == null)
            throw new ArgumentNullException("request");

        if (callback == null)
            throw new ArgumentNullException("callback");

        request.BeginGetResponse((IAsyncResult result) =>
        {
            try
            {
                var response = request.EndGetResponse(result);
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    callback(reader.ReadToEnd());
                }
            }
            catch (WebException e)
            {
                // Don't perform a callback, as this error is mostly due to
                // there being no internet connection available. 
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
        }, request);
    }
}
like image 200
Claus Jørgensen Avatar answered Mar 07 '23 00:03

Claus Jørgensen


The issue I referred to was that in 7.0 WebClient always returned on the UI thread regardless of where it was created, potentially making the UI unresponsive.

In the WP SDK 7.1 WebClient will return on the thread it was created from, so if it is created from a background thread DownloadStringCompleted will now return on a background thread.

If you test my example without marshalling the response you will see an Invalid Cross Thread Exception.

It seems to me unless you have a reason not to, why not use WebClient now?

like image 42
Jim Avatar answered Mar 07 '23 00:03

Jim