【发布时间】:2016-01-24 10:28:53
【问题描述】:
我有一个网页,其中包含添加设备的表单。
当用户添加设备时,设备会在 4 个不同的地方注册。 由于这 4 个注册中的每一个都需要时间,因此我决定使用异步调用。
因此,当用户单击保存按钮时,会向服务器触发 AJAx 请求并调用“保存”方法。 “Save”方法有一个循环,异步调用“Register”方法,如下所示:
public delegate bool DeviceControllerAsync(...);
public static string Save(...)
{
//Get all active controllers
List<Controller> lstControllers = Controller.Get();
foreach (Controller controller in lstControllers)
{
// Invoke asynchronous write method
DeviceControllerAsync caller = new DeviceControllerAsync(ArubaBL.RegisterDevice);
// Initiate the asychronous call.
IAsyncResult result = caller.BeginInvoke(..., null, null);
}
return GetRegisteredDevices();
}
这里的问题是“GetRegisteredDevices”调用是没有意义的,因为异步方法还没有完成,并且没有设备可以返回。 此外,当这些操作完成后,我无法更新 UI,因为 main 方法已经返回到 UI。
(如果用户在单击“保存”按钮后立即移动另一个页面,我将忽略此情况。)
那么,有没有办法让我知道所有异步调用何时完成,然后调用将更新 UI 的方法?
【问题讨论】:
-
你能迁移到 TPL 来代替旧式的 begin/endinvoke 吗?
-
this 可能有帮助
-
如果我知道 TPL 是什么,我可以迁移到 TPL...什么是 TPL ???
-
我认为async/await 会比基本的 TPL 更容易...请更新您帖子上的标签以指定您使用的框架(除了普通的 ASP.Net 之外的任何东西都可以轻松使用 async/await)
标签: c# asynchronous web .net-4.0 entity-framework-4