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

标签 c# asp.net-mvc asynchronous

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

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/

相关文章:

c# - Action 过滤器 Action 参数

ios - 同步打开 UIDocument

javascript - Promise.all 错误处理——让一个 Promise 的结果在另一个 Promise 的 catch 中可访问?

c# - 可空日期时间到字符串

c# - 删除时出现 WebAPI 405 错误

c# - 反序列化C#中的对象列表

asp.net - 将网站从 ASP.NET 迁移到 ASP.NET MVC

c# - 如果页面在 using block 中重定向,DB Context 会自动处理吗?

c# - Linq & boolean 函数

node.js - 限制 Node.js 中的异步调用