C# How do I pass more than just IAsyncResult into AsyncCallback?

本小妞迷上赌 提交于 2019-12-03 15:48:10

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..
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!