How do I pass more than just the IAsyncResult into AsyncCallback?
Example code:
//Usage
var req = (HttpWebRequest)iAreq;
req.BeginGetResponse(new AsyncCallback(iEndGetResponse), req);
//Method
private void iEndGetResponse(IAsyncResult iA, bool iWantInToo) { /*...*/ }
I would like to pass in example variable bool iWantInToo
. I don't know how to add that to new AsyncCallback(iEndGetResponse)
.
You have to use the object state to pass it in. Right now, you're passing in the req
parameter - but you can, instead, pass in an object containing both it and the boolean value.
For example (using .NET 4's Tuple - if you're in .NET <=3.5, you can use a custom class or KeyValuePair, or similar):
var req = (HttpWebRequest)iAreq;
bool iWantInToo = true;
req.BeginGetResponse(new AsyncCallback(iEndGetResponse), Tuple.Create(req, iWantInToo));
//Method
private void iEndGetResponse(IAsyncResult iA)
{
Tuple<HttpWebRequest, bool> state = (Tuple<HttpWebRequest, bool>)iA.AsyncState;
bool iWantInToo = state.Item2;
// use values..
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With