How do I use the Task.Factory.FromAsync factory for an end method that returns multiple values via "out" parameters?
The begin method has this signature:
public virtual System.IAsyncResult BeginGetCaseStatus(int CaseOID, int ClientOID, System.AsyncCallback @__Callback, object @__UserData)
End method is:
public virtual void EndGetCaseStatus(System.IAsyncResult @__AsyncResult, out DTGenericCode[] BasicStatus, out DTGenericCode[] ARStatus)
Can I some how use:
public Task<?> GetCaseStatusAsync(int CaseOID, int ClientOID)
{
return Task.Factory.FromAsync (BeginGetCaseStatus(CaseOID, ClientOID, null, null), EndGetCaseStatus(?, ?));
}
There is no FromAsync
overload that would work for this. The best option would likely be to build your own wrapper:
public class ResultStatus
{
public ResultStatus(DTGenericCode[] basicStatus, DTGenericCode[] arStatus)
{
this.BasicStatus = basicStatus;
this.ARStatus = arStatus;
}
public DTGenericCode[] BasicStatus { get; private set; }
public DTGenericCode[] ARStatus { get; private set; }
}
public Task<ResultStatus> GetCaseStatusAsync(int CaseOID, int ClientOID)
{
var tcs = new TaskCompletionSource<ResultStatus>();
theClass.BeginGetCaseStatus(CaseOID, ClientOID, iar =>
{
DTGenericCode[] bs;
DTGenericCode[] as;
theClass.EndGetCaseStatus(iar, out bs, out as);
tcs.SetResult(new ResultStatus(bs, as));
}, null);
return tcs.Task;
}
The endMethod
parameter of FromAsync()
is a delegate, so you can use a lambda to convert the out
-form to the normal form of -End
methods. Something like:
private static Task<Tuple<DTGenericCode[], DTGenericCode[]>> GetCaseStatusAsync(
int CaseOID, int ClientOID)
{
return Task.Factory.FromAsync(
BeginGetCaseStatus, ar =>
{
DTGenericCode[] basicStatus;
DTGenericCode[] arStatus;
EndGetCaseStatus(ar, out basicStatus, out arStatus);
return Tuple.Create(basicStatus, arStatus);
},
CaseOID, ClientOID, null);
}
Though you probably want to use a custom type instead of the Tuple
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With