如何将Task.Factory.FromAsync工厂用于通过“out”参数返回多个值的end方法?
begin方法有这个签名:
public virtual System.IAsyncResult BeginGetCaseStatus(int CaseOID, int ClientOID, System.AsyncCallback @__Callback, object @__UserData)
结束方法是:
public virtual void EndGetCaseStatus(System.IAsyncResult @__AsyncResult, out DTGenericCode[] BasicStatus, out DTGenericCode[] ARStatus)
我可以使用一些方法:
public Task<?> GetCaseStatusAsync(int CaseOID, int ClientOID)
{
return Task.Factory.FromAsync (BeginGetCaseStatus(CaseOID, ClientOID, null, null), EndGetCaseStatus(?, ?));
}
答案 0 :(得分:6)
没有FromAsync
重载对此有用。最好的选择可能是构建自己的包装器:
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;
}
答案 1 :(得分:5)
endMethod
的{{1}}参数是委托,因此您可以使用lambda将FromAsync()
- 表单转换为out
方法的正常形式。类似的东西:
-End
虽然您可能希望使用自定义类型而不是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);
}
。