Two questions:
How do I pass arguments to the
AsyncCallback
method?How do I invoke the
AsyncCallback
method?
Here's the background:
I am working on an old project in .NET Framework 3.5. I want to create an async method doing some works without blocking current thread. Since the project is an old version Async Await key words are not available, I plan to do all parallel works including some database queries and api calling in AsyncCallback method. I know AsyncState can be used to convey arguments. Because I need multiple arguments, I plan to include these arguments into an object and set the object to AsyncState, so I can cast AsyncState as the object to be used in AsyncCallback method. I didn't find a good way. I've found this link, but I don't use HttpWebRequest.
public Class ArgumentObject
{
public string A{get;set;}
public string B{get;set;}
...
}
public void CallBack(IAsyncResult result, ArgumentObject arObj)
{
Console.WriteLn(arObj.A);
Console.WriteLn(arObj.B);
}
The following are the code which call the AsyncCallBack method.
var argumentsObj=new ArgumentObject{ A="argument1", B="argument2"..};
AsyncCallback callback = (IAsyncResult result) => CallBack(result, argumentsObj);
// How do I know it is triggered? I set the break point into CallBack method, but it never goes to there.
Any idea or correction is appreciated.