After searching in the Net for about 4 hours I still don’t understand Async functions on Windows Phone 7. I tried to run that code but it looks like the event “DownloadStringCompleted” for my webClient is never raised. I tried to wait here for an answer, but it just freeze my app. Anyone could help and explain why it don’t work?
internal string HTTPGet()
{
string data = null;
bool exit = false;
WebClient webClient = new WebClient();
webClient.UseDefaultCredentials = true;
webClient.DownloadStringCompleted += (sender, e) =>
{
if (e.Error == null)
{
data = e.Result;
exit = true;
}
};
webClient.DownloadStringAsync(new Uri(site, UriKind.Absolute));
//while (!exit)
// Thread.Sleep(1000);
return data;
}
Ok. Found something!
http://blogs.msdn.com/b/kevinash/archive/2012/02/21/async-ctp-task-based-asynchronous-programming-for-windows-phone.aspx
Yay! 🙂
Its not a problem with emulator. you want to return the data from your HttpGet() method, but the data is already returned (as null) before the actual response from the webClient occurs. hence I suggest you to make some changes to the code and try.
and then in the DownloadCompleted event handler(or callback), you manupulate the actual result