我在我正在处理的网站上有一个登陆页面,在调用该页面的操作时,进行了一些异步网络调用,以便缓存结果供以后使用。不过,我想要做的是等待调用完成,然后再进行下一步操作。基本上我有:
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 吗?
我是一名优秀的程序员,十分优秀!