gpt4 book ai didi

c# - 异步调用 - 如何在 UI 线程上等待它们

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:53 25 4
gpt4 key购买 nike

我在我正在处理的网站上有一个登陆页面,在调用该页面的操作时,进行了一些异步网络调用,以便缓存结果供以后使用。不过,我想要做的是等待调用完成,然后再进行下一步操作。基本上我有:

GetParticipantInfo(planID, partID );

SetCurrentInvestments(partID, planID);

GetLoanFunds(planID, partID);

每一个都像这样拆分:

public void GetParticipantInfo(string planNumber, string participantID)
{
IAsyncResult _IAsyncResult;
List<string> parameter = new List<string>();
parameter.Add(planNumber);
parameter.Add(participantID);
GetParticipantInfo_A _GetParticipantInfo_A = new GetParticipantInfo_A(GetParticipantInfoAsync);
_IAsyncResult = _GetParticipantInfo_A.BeginInvoke(participantID, planNumber, serviceContext, GetParticipantInfoAsyncCallBack, parameter);

}

public ParticipantDataModel GetParticipantInfoAsync(string planNumber, string partId, ServiceContext esfSC)
{

ParticipantDataModel pdm = new ParticipantDataModel();
return pdm;

}

private void GetParticipantInfoAsyncCallBack(IAsyncResult ar)
{
try
{
AsyncResult result;
result = (AsyncResult)ar;
string planID = ((List<string>)ar.AsyncState)[0];
GetParticipantInfo_A caller = (GetParticipantInfo_A)result.AsyncDelegate;
ParticipantDataModel pdm = caller.EndInvoke(ar);
_cacheManager.SetCache(planID, CacheKeyName.GetPartInfo.ToString(), pdm);
}
catch (Exception ex)
{ }
}

所以问题是,如何设置 UI 线程以等待调用完成,然后再继续执行其他操作?

回应乔:

好吧,假设他们都返回异步结果,我可以这样做吗:

List<IAsyncResult> results;
//After each call
result = OneOfTheAsyncCalls();
results.Add(result);

foreach(IAsyncResult result in results)
{
result.AsyncWaitHandle.WaitOne();
}

或者顺序是否重要?

最佳答案

你能在调用者和异步回调之间使用 AutoResetEvent 吗?

关于c# - 异步调用 - 如何在 UI 线程上等待它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10686604/

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