Suppose I have the following method signature for an Async method:
public IAsyncResult MyAsyncMethod(AsyncCallback asyncCallback, object state);
And, therefore, I use the following code to call it and assign it a state object and a callback:
public class test
{
// Private class to hold / pass-through data used by Async operations
private class StateData
{
//... Whatever properties I'll need passed to my callback ...
}
public void RunOnSeparateThread()
{
StateData stateData = new StateData { ... };
MyAsyncMethod(m_asyncCallback, stateData);
}
private AsyncCallback m_asyncCallback = new AsyncCallback(AsyncComplete);
private static void AsyncComplete(IAsyncResult result)
{
// Whatever stuff needs to be done after completion of Async method
// using data returned in the stateData object
}
}
I'm looking to do the same thing using Task Parallel Library and am not certain how to do it. What would be the correct code to achieve the same result, being:
- Run
MyAsyncMethodon a separate thread - On completion have a
stateobject returned with it passed to the next method - In this caseAsyncComplete
Or is there a better way to accomplish the same result?
Also, although this is written in C#, I'm equally comfortable in VB and C#, so any answer would be greatly appreciated.
Thank you and hopefully this makes sense.