Reputation: 704
Maybe simple question but how can I get the return object of a asynchronously System.Func?
Consider this:
private void Start(String a)
{
System.Func<String, objectA> callasync = delegate(String strA)
{
return bla(stra); // Which return a objectA
}
callasync.BeginInvoke(a, callback, null);
}
private void callback(IAsyncResult ar)
{
// Here I want the objectA but how ??
}
With callback function, I didn't have the delegate signature. And of course, i can create a delegate in scope of the class but maybe is there a solution to read the return value in the callback function.
Thank.
Upvotes: 0
Views: 979
Reputation: 22693
When you use BeginInvoke
on a delegate, the IAsyncResult
of passed to the callback will be an instance of the AsyncResult
class. From that you can get an instance of your delegate and call EndInvoke
on it.
private void Start(string a)
{
Func<string, objectA> d = strA => bla(strA);
d.BeginInvoke(a, Callback, null);
}
private void Callback(IAsyncResult ar)
{
AsyncResult asyncResult = (AsyncResult)ar;
Func<string, objectA> d = (Func<string, objectA>)asyncResult.AsyncDelegate;
objectA result = d.EndInvoke(ar);
}
Upvotes: 1
Reputation: 5825
You could also do something like this (not written in IDE, may contain errors)
private Func<String, objectA> _callDoWorkAsync = DoWork;
private static string DoWork( objectA strA )
{
return bla( strA );
}
private void Start( String a )
{
_callDoWorkAsync.BeginInvoke( a, callback, null );
}
private void callback( IAsyncResult ar )
{
objectA strA = (objectA) _callDoWorkAsync.EndInvoke( ar );
}
Upvotes: 0