I'm looking to use an asynchronous web request in silverlight and would like to look at an example which isn't as confusing as the documentation on msdn.
views:
29answers:
1
+4
A:
The secret of asynchronous method calls is that you provide a callback method (defined inline as lambda expression below) and then call the Async method which will immediately return. When the asynchronous operation finished, the callback method will be called.
var wc = new WebClient();
wc.DownloadStringCompleted += (sender, e) =>
{
using (sender as IDisposable)
{
myTextBox.Text = e.Result;
}
};
wc.DownloadStringAsync(new Uri("http://stackoverflow.com"));
var wc = new WebClient();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri("http://stackoverflow.com"));
with
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
using (sender as IDisposable)
{
myTextBox.Text = e.Result;
}
}
dtb
2010-08-30 14:53:13
sorry I'm not too good at reading lambdas...what would it look like without the lambda?
cfarm54
2010-08-30 18:46:40
@cfarm54: A lambda expression is just a shorthand way to write a method. I've added a version without lambda expression. Both versions do the same.
dtb
2010-08-30 19:48:18
@dtb oh cool. thanks. i'll take a look. i'm ultimately trying to download a csv file through the yahoo api then display it in a silverlight chart. do you think that's possible using this methodology?
cfarm54
2010-08-30 19:54:40
@cfarm54: Apart from URL Access Restrictions this should work. http://msdn.microsoft.com/en-us/library/cc189008.aspx
dtb
2010-08-30 20:30:30