Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C-如何在异步方法调用后更新网页?_C#_Asynchronous_Web_.net 4.0_Entity Framework 4 - Fatal编程技术网

C# C-如何在异步方法调用后更新网页?

C# C-如何在异步方法调用后更新网页?,c#,asynchronous,web,.net-4.0,entity-framework-4,C#,Asynchronous,Web,.net 4.0,Entity Framework 4,我有一个网页,其中有一个表单可以添加设备 当用户添加设备时,该设备将在4个不同的位置注册。 因为这4个注册都需要时间,所以我决定使用异步调用 因此,当用户单击save按钮时,会向服务器发出AJAx请求并调用save方法。 Save方法有一个异步调用Register方法的循环,如下所示: public delegate bool DeviceControllerAsync(...); public static string Save(...) { //Get all active co

我有一个网页,其中有一个表单可以添加设备

当用户添加设备时,该设备将在4个不同的位置注册。 因为这4个注册都需要时间,所以我决定使用异步调用

因此,当用户单击save按钮时,会向服务器发出AJAx请求并调用save方法。 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库和async/Wait关键字的简化示例

public static async string Save(...)
{
    //Get all active controllers
    List<Controller> lstControllers = Controller.Get();

    //Create a task object for each async task
    List<Task<returnValueType>> controllerTasks = lstControllers.Select(controller=>{
        DeviceControllerAsync caller = new DeviceControllerAsync(ArubaBL.RegisterDevice);
        return Task.Factory.FromAsync<returnValueType>(caller.BeginInvoke, caller.EndInvoke, null);
    }).ToList();

    // wait for tasks to complete (asynchronously using await)
    await Task.WhenAll(controllerTasks);

    //Do something with the result value from the tasks within controllerTasks
}

这是一个使用TPL库和async/await关键字的简化示例

public static async string Save(...)
{
    //Get all active controllers
    List<Controller> lstControllers = Controller.Get();

    //Create a task object for each async task
    List<Task<returnValueType>> controllerTasks = lstControllers.Select(controller=>{
        DeviceControllerAsync caller = new DeviceControllerAsync(ArubaBL.RegisterDevice);
        return Task.Factory.FromAsync<returnValueType>(caller.BeginInvoke, caller.EndInvoke, null);
    }).ToList();

    // wait for tasks to complete (asynchronously using await)
    await Task.WhenAll(controllerTasks);

    //Do something with the result value from the tasks within controllerTasks
}

您是否可以迁移到TPL而不是旧式的begin/ENDPINKER?如果我知道TPL是什么,我可以迁移到TPL。。。什么是第三方物流???第三方物流:一个简单的谷歌搜索离开我认为会比基本的第三方物流更容易。。。请更新帖子上的标签,以指定除了普通ASP.Net之外,其他任何东西都可以轻松使用的框架async/Await您可以迁移到TPL而不是老式的begin/endinvoke吗?如果我知道TPL是什么,我可以迁移到TPL。。。什么是第三方物流???第三方物流:一个简单的谷歌搜索离开我认为会比基本的第三方物流更容易。。。请在您的帖子上更新标记,以指定您使用的框架,而不是简单的ASP.Net将很容易使用async/Await。您知道为什么这段代码不能编译吗System.Threading.Tasks.Task'不包含“WhenAll”的定义@Liran是的,Task.WhenAll在.NET 4上不可用。试试NuGet的包。我正在使用Visual Studio 2010。不幸的是:VisualStudio2010不支持此软件包,并且仅在目标项目中需要此软件包。NET Framework 4.5、Windows 8、Windows Phone Silverlight 8或Windows Phone 8.1使用使用此软件包的库时。我可以改用Task.Factory.ContinueWhenAll吗?@Liran ContinueWhenAll不会同步等待。您可以使用Task.WaitAll,它正是这样做的。注意,它将阻止调用线程,直到所有任务完成。你知道为什么这段代码不能编译吗System.Threading.Tasks.Task'不包含“WhenAll”的定义@Liran是的,Task.WhenAll在.NET 4上不可用。试试NuGet的包。我正在使用Visual Studio 2010。不幸的是:VisualStudio2010不支持此软件包,并且仅在目标项目中需要此软件包。NET Framework 4.5、Windows 8、Windows Phone Silverlight 8或Windows Phone 8.1使用使用此软件包的库时。我可以改用Task.Factory.ContinueWhenAll吗?@Liran ContinueWhenAll不会同步等待。您可以使用Task.WaitAll,它正是这样做的。注意,它将阻止调用线程,直到所有任务完成。