21

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.

Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
  • 3
    Re IMDB: terms page: "Robots and Screen Scraping: You may not use data mining, robots, screen scraping, or similar data gathering and extraction tools on this site, except with our express written consent as noted below.". I **strongly suggest** that you do **not** this; it is clearly against their rules. – Marc Gravell Oct 18 '09 at 20:53
  • 1
    Marc, what other site has information like IMDB that I CAN use? Thanks for the help. – Sergio Tapia Oct 18 '09 at 21:22
  • @Sergio http://developer.rottentomatoes.com/ – Colin Pickard Apr 04 '13 at 08:48
  • @SergioTapia also, themoviedb.org and thetvdb.com which I believe both have APIs you can use to access the data directly and save yourself alot of time. ;) – Arvo Bowen Feb 24 '16 at 16:38

6 Answers6

40

There is a newer DownloadDataTaskAsync method that allows you to await the result. It is simpler to read and easier to wire up by far. I'd use that...

var client = new WebClient();

var data = await client.DownloadDataTaskAsync(new Uri(imageUrl));

await outstream.WriteAsync(data, 0, data.Length);
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Bill Forney
  • 786
  • 7
  • 16
  • But do this is not the same to do the call in a sync way? (It is DownloadDataTask and in this case you will receive the result directly), if you want to do an async call I think this is not an option, because is working like sync. – Daniel Silva Nov 22 '17 at 12:41
  • 1
    It uses async/await with the Task class. It is asynchronous as much as the above answer is. The only difference is you use a continuation method DownloadDataCompleted in the above and you use an await to inline the continuation in my version. If you wanted progress reporting or the like I might do it differently but if you just want a continuation on completion this way works just as well with less code. – Bill Forney Nov 23 '17 at 23:19
  • 2
    If you want to do other things while the Task is running you just don't await it and do your stuff, then add a continuation to it. [Check this out for info](https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/chaining-tasks-by-using-continuation-tasks). – Bill Forney Nov 23 '17 at 23:25
31

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 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 – H H Oct 18 '09 at 21:47
  • 1
    as Bill Forney said, DownloadDataTaskAsync is a better solution – hvojdani Sep 26 '19 at 12:49
5
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);
}
Remy
  • 12,555
  • 14
  • 64
  • 104
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
3

If anyone using above in web application or websites please set Async = "true" in the page directive declaration in aspx file.

ssmsnet
  • 2,226
  • 2
  • 17
  • 14
1
ThreadPool.QueueUserWorkItem(state => WebClientX.DownloadDataAsync(sitesearchURL));

http://workblog.pilin.name/2009/02/system.html

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
OnicDr
  • 11
  • 1
0

//using ManualResetEvent class

static ManualResetEvent evnts = new ManualResetEvent(false);
static void Main(string[] args)
{
    byte[] data = null;
    WebClient client = new WebClient();
    client.DownloadDataCompleted += 
        delegate(object sender, DownloadDataCompletedEventArgs e)
        {
             data = e.Result;
             evnts.Set();
        };
    Console.WriteLine("starting...");
    evnts.Reset();
    client.DownloadDataAsync(new Uri("http://stackoverflow.com/questions/"));
    evnts.WaitOne(); // wait to download complete

    Console.WriteLine("done. {0} bytes received;", data.Length);
}
Hung Le
  • 49
  • 2