views:

281

answers:

2

Hey all, first time poster, long time reader, so take it easy on me :)

I'm writing an application that reads in a bunch of folders and allows you to download an image for each folder. There is a WebBrowser control where you can browse to a particular image, and click a button "use this image for the selected folder".

Each "folder" is represented as an object with its own download method, which uses WebClient inside a BackgroundWorker Thread.

When I set the image url on the object, it starts the backgroundworker of that object and runs this code

        using (WebClient client = new WebClient())
        {
            client.Proxy = null;
            Stream stream = client.OpenRead(BackdropUrl);
            Bitmap bitmap = new Bitmap(stream);
            stream.Flush();
            stream.Close();
            e.Result = bitmap;
        }

I also use a webclient in my applications main thread to do some simple html "scraping".

Now, to the problem. When I start using the app, all is good, the files will download as I go through the list, but after a while, it seems that the webclients randomly become unresponsive. I can still browse around in the webbrowser control, but all the webclient execution code seems to not complete it just hangs at one of these two lines:

Stream stream = client.OpenRead(BackdropUrl);
Bitmap bitmap = new Bitmap(stream);

I know this is still pretty vague, but I tried to explain it as best I could. Does anyone maybe have a more robust way to download an image straight into a Bitmap object? i.e. not saving to file then opening it again?

Any help will be greatly appreciated.

A: 

In the code snippet posted, you don't appear to be disposing of the BitMap objects you create

Mitch Wheat
+1  A: 

WebClient does not support user-specified timeouts, and (IIRC) has a default timeout of 100,000 msecs (100 seconds). Try waiting for >100 secs and see if you get any exceptions.

If that is the issue, you'll probably need to refactor to use the HTTPWebRequest and HTTPWebResponse classes instead on WebClient and configure a more reasonable timeout.

Addys