DownloadStringAsync wait for request completion

前端 未结 6 1359
感动是毒
感动是毒 2020-12-10 13:41

I am using this code to retrieve an url content:

private ArrayList request(string query)
{
    ArrayList parsed_output = new ArrayList();

    string url = s         


        
相关标签:
6条回答
  • 2020-12-10 14:03

    From the msdn documentation:

    When the download completes, the DownloadStringCompleted event is raised.

    When hooking this event, you will receive DownloadStringCompletedEventArgs, this contains a string property Result with the resulting string.

    0 讨论(0)
  • 2020-12-10 14:04

    With Dot.Net 4.5:

     public static async void GetDataAsync()
            {           
                DoSomthing(await new WebClient().DownloadStringTaskAsync(MyURI));
            }
    
    0 讨论(0)
  • 2020-12-10 14:04

    this will keep your gui responsive, and is easier to understand IMO:

    public static async Task<string> DownloadStringAsync(Uri uri, int timeOut = 60000)
    {
        string output = null;
        bool cancelledOrError = false;
        using (var client = new WebClient())
        {
            client.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error != null || e.Cancelled)
                {
                    cancelledOrError = true;
                }
                else
                {
                    output = e.Result;
                }
            };
            client.DownloadStringAsync(uri);
            var n = DateTime.Now;
            while (output == null && !cancelledOrError && DateTime.Now.Subtract(n).TotalMilliseconds < timeOut)
            {
                await Task.Delay(100); // wait for respsonse
            }
        }
        return output;
    }
    
    0 讨论(0)
  • 2020-12-10 14:10

    i had the same problem with WP7 i solved this method.await is not working wp7 but if you use Action you will callback Async functions

         public void Download()
         { 
             DownloadString((result) =>
             {
                 //!!Require to import Newtonsoft.Json.dll for JObject!!
                 JObject fdata= JObject.Parse(result);
                 listbox1.Items.Add(fdata["name"].ToString());
    
             }, "http://graph.facebook.com/zuck");
    
        }
    
        public void DownloadString(Action<string> callback, string url)
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += (p, q) =>
            {
                if (q.Error == null)
                {
                    callback(q.Result);
    
                }
    
            };
    
            client.DownloadStringAsync(new Uri(url));
    
        }
    
    0 讨论(0)
  • 2020-12-10 14:16

    You really want to WAIT for an async method to complete after you launch it, in the same thread? Why not just use the sync version then.

    You should hook up the DownloadStringCompleted event and catch the result there. Then you can use it as a real Async method.

    0 讨论(0)
  • 2020-12-10 14:22

    why would you want to wait... that will block the GUI just as before!

    var client = new WebClient();
    client.DownloadStringCompleted += (sender, e) => 
    {
       doSomeThing(e.Result);
    };
    
    client.DownloadStringAsync(uri);
    
    0 讨论(0)
提交回复
热议问题