问题描述
如何传递的不仅仅是更多的IAsyncResult到的AsyncCallback?
How do I pass more than just the IAsyncResult into AsyncCallback?
举例code:
//Usage
var req = (HttpWebRequest)iAreq;
req.BeginGetResponse(new AsyncCallback(iEndGetResponse), req);
//Method
private void iEndGetResponse(IAsyncResult iA, bool iWantInToo) { /*...*/ }
我想例如变量布尔iWantInToo
中传递。我不知道如何将它添加到新的AsyncCallback(iEndGetResponse)
。
I would like to pass in example variable bool iWantInToo
. I don't know how to add that to new AsyncCallback(iEndGetResponse)
.
推荐答案
您必须使用对象的状态,它传递,现在,你传递在 REQ
参数 - 但你可以,相反,通过在包含它和布尔值的对象。
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.
例如(使用.NET 4的元组 - 如果你在.NET< = 3.5,则可以使用自定义类或KeyValuePair或类似):
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..
}
这篇关于C#如何传递的不仅仅是更多的IAsyncResult到的AsyncCallback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!