locked
Using the ASync download method? RRS feed

  • Question

  • User-885817678 posted

    My code looks like this:

    private static string GetJSONData(string url)
    		{
    			string jsonData = string.Empty;
    			int attempts = 1;
    			Uri uriUrl = new Uri(url);
    			WebClient client = new WebClient();
    			while (attempts <= 5)
    			{
    				try
    				{
    					client.DownloadStringCompleted += client_DownloadStringCompleted;
    					client.DownloadStringAsync(uriUrl, jsonData);
    					return jsonData.ToString();
    				}
    				catch
    				{
    					attempts++;
    				}
    			}
    			return "failed";
    		}
    
    		static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    		{
    		}

    The problem is I am unsure how to make the app wait until the download is complete to return the data.  Right now it juts executes the return statement and then throws a null reference exception.

    Saturday, October 11, 2014 7:40 PM

Answers

All replies