我正在处理的网站上有一个登录页面,并且在调用该页面的操作时,会进行一些异步 Web 调用以缓存结果以供以后使用。不过,我想做的是等待调用完成,然后再进行下一个操作。基本上我有:
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 线程以等待调用完成,然后再进行其他操作?
回应乔:
好的,所以假设他们都返回 asyncresult,我可以做类似的事情:
List<IAsyncResult> results;
//After each call
result = OneOfTheAsyncCalls();
results.Add(result);
foreach(IAsyncResult result in results)
{
result.AsyncWaitHandle.WaitOne();
}
还是顺序很重要?