Reputation: 263
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!
Upvotes: 0
Views: 489
Reputation: 1499730
The simplest way to do this is to use a lambda expression. Something like this:
absRequest.BeginGetResponse(result => OnFetchComplete(result, foo, absRequest),
null);
where OnFetchComplete
now has the signature you really want (with the extra parameters - in this case one corresponding to foo
) rather than just an IASyncResult
. You no longer need to give absRequest
as the "context" for the IAsyncResult
, 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.
Upvotes: 2