gpt4 book ai didi

c# - 结束方法中带有 "out"参数的 Task.Factory.FromAsync

转载 作者:太空狗 更新时间:2023-10-29 22:19:16 24 4
gpt4 key购买 nike

如何将 Task.Factory.FromAsync 工厂用于通过“out”参数返回多个值的结束方法?

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(?, ?));
}

最佳答案

没有适用于此的 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;
}

关于c# - 结束方法中带有 "out"参数的 Task.Factory.FromAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com