Ok I am using the HttpWebRequest BeginGetResponse and I pass in the async callback function, now I want to pass in a variable/context as well that I want to my callback function to get. How do I do this. I am fairly new to the C#/.Net/Silverlight world.
HttpWebRequest absRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlToFetch));
absRequest.BeginGetResponse(new AsyncCallback(onABSFetchComplete), absRequest);
here my objective is in the above call I want to pass a variable and I want my callback to get called with that variable.
thanks!
The simplest way to do this is to use a lambda expression. Something like this:
where
OnFetchCompletenow has the signature you really want (with the extra parameters – in this case one corresponding tofoo) rather than just anIASyncResult. You no longer need to giveabsRequestas the “context” for theIAsyncResult, as you’re capturing it in the lambda expression.If you’re not familiar with lambda expressions though, you should really take time to get to grips with them though – not just for this, but for LINQ and all kinds of other purposes.