I am using this code to retrieve an url content:
private ArrayList request(string query)
{
ArrayList parsed_output = new ArrayList();
string url = string.Format(
"http://url.com/?query={0}",
Uri.EscapeDataString(query));
Uri uri = new Uri(url);
using (WebClient client = new WebClient())
{
client.DownloadStringAsync(uri);
}
// how to wait for DownloadStringAsync to finish and return ArrayList
}
I want to use DownloadStringAsync
because DownloadString
hangs the app GUI, but I want to be able to return the result on request
. How can I wait until DownloadStringAsync
finish the request?
With Dot.Net 4.5:
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
From the msdn documentation:
When hooking this event, you will receive DownloadStringCompletedEventArgs, this contains a
string
propertyResult
with the resulting string.why would you want to wait... that will block the GUI just as before!
this will keep your gui responsive, and is easier to understand IMO:
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.