How to use the WebClient.DownloadDataAsync() method in this context?

前端 未结 6 1656
北荒
北荒 2020-12-01 18:01

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.

Her

相关标签:
6条回答
  • 2020-12-01 18:43

    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);
    
    0 讨论(0)
  • 2020-12-01 18:44
    ThreadPool.QueueUserWorkItem(state => WebClientX.DownloadDataAsync(sitesearchURL));
    

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

    0 讨论(0)
  • 2020-12-01 18:48
    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);
    }
    
    0 讨论(0)
  • 2020-12-01 18:57

    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.

    0 讨论(0)
  • 2020-12-01 18:59

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

    0 讨论(0)
  • 2020-12-01 19:06

    //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);
    }
    
    0 讨论(0)
提交回复
热议问题