views:

840

answers:

2

My plan is to have a user write down a movie title in my program and my program will pull the appropiate information asynchronously so the UI doesn't freeze up.

Here's the code:

public class IMDB
    {
        WebClient WebClientX = new WebClient();
        byte[] Buffer = null;


        public string[] SearchForMovie(string SearchParameter)
        {
            //Format the search parameter so it forms a valid IMDB *SEARCH* url.
            //From within the search website we're going to pull the actual movie
            //link.
            string sitesearchURL = FindURL(SearchParameter);

            //Have a method download asynchronously the ENTIRE source code of the
            //IMDB *search* website.
            Buffer = WebClientX.DownloadDataAsync(sitesearchURL);


            //Pass the IMDB source code to method findInformation().

            //string [] lol = findInformation();

            //????

            //Profit.

            string[] lol = null;
            return lol;
        }

My actual problem lies in the WebClientX.DownloadDataAsync() method. I can't use a string URL for it. How can I use that built in function to download the bytes of the site (for later use I will convert this to string, I know how to do this) and without freezing up my GUI?

Perhaps a clear cut example of the DownloadDataAsync so I can learn how to use it?

Thanks SO, you're always such a great resource.

+2  A: 

You need to handle the DownloadDataCompleted event:

static void Main()
{
    string url = "http://google.com";
    WebClient client = new WebClient();
    client.DownloadDataCompleted += DownloadDataCompleted;
    client.DownloadDataAsync(new Uri(url));
    Console.ReadLine();
}

static void DownloadDataCompleted(object sender,
    DownloadDataCompletedEventArgs e)
{
    byte[] raw = e.Result;
    Console.WriteLine(raw.Length + " bytes received");
}

The args contains other bits of information relating to error conditions etc - check those too.

Also note that you'll be coming into DownloadDataCompleted on a different thread; if you are in a UI (winform, wpf, etc) you'll need to get to the UI thread before updating the UI. From winforms, use this.Invoke. For WPF, look at the Dispatcher.

Marc Gravell
Just to dot-the-i, there is a standard pattern for handling `DownloadDataCompleted`, like for `RunWorkerCompleted`, see http://msdn.microsoft.com/en-us/library/cc221403%28VS.95%29.aspx
Henk Holterman
+1  A: 

Hi, try this:

static void Main(string[] args)
{
    byte[] data = null;
    WebClient client = new WebClient();
    client.DownloadDataCompleted += 
        delegate(object sender, DownloadDataCompletedEventArgs e)
        {
             data = e.Result;
        };
    Console.WriteLine("starting...");
    client.DownloadDataAsync(new Uri("http://stackoverflow.com/questions/"));
    while (client.IsBusy)
    {
        Console.WriteLine("\twaiting...");
        Thread.Sleep(100);
    }
    Console.WriteLine("done. {0} bytes received;", data.Length);
}
Rubens Farias