WPF异步等待任务锁定UI线程并行运行任务



我有一个WPF应用程序,点击按钮后,它会创建一个List<Task<int>>并启动这些任务。我的假设是Add()调用以并行但异步的方式启动这些程序。

这是我的函数,它在远程机器上以串行的方式执行一系列WMI调用:

代理引导程序.cs

public async Task<int> BootstrapAsync(BootstrapContext context, IProgress<BootstrapAsyncProgress> progress)
{
  ...
  do a bunch of stuff in serial *without* await calls
  ...
  if (progress != null)
  {
      progress.Report(new BootstrapAsyncProgress
      {
          MachineName = context.MachineName, 
          ProgressPercentage = 30, 
          Text = "Copying install agent software to \\" + context.MachineName + "\" + context.ShareName
      });
  }
  ...
  return pid; // ProcessId of the remote agent that was just started
}

这显然是我在UI中的按钮处理程序:

壳牌.xaml.cs

private async void InstallButton_Click(object sender, RoutedEventArgs e)
{
    var bootstrapTasks = new List<Task<int>>();
    var progress = new Progress<BootstrapAsyncProgress>();
    progress.ProgressChanged += (o, asyncProgress) =>
    {
        Debug.WriteLine("{0}: {1}% {2}", asyncProgress.MachineName, asyncProgress.ProgressPercentage,
            asyncProgress.Text);
        //TODO Update ViewModel property for ProgressPercentage
    };
    var vm = DataContext as ShellViewModel;
    Debug.Assert(vm != null);
    foreach (var targetMachine in vm.TargetMachines)
    {
        var bootstrapContext = new BootstrapContext(targetMachine.MachineName, true)
        {
            AdminUser = vm.AdminUser,
            AdminPassword = vm.AdminPassword
        };
        var bootstrapper = new AgentBootstrapper(bootstrapContext);
        bootstrapTasks.Add(bootstrapper.BootstrapAsync(bootstrapContext, progress)); // UI thread locks up here
    }
}

我知道标记为async的函数中应该有使用await的函数调用。在我的例子中,这些都是对一些同步WMi助手函数的调用,这些函数都返回void。所以,我认为await不是我想要的。

简单地说,我希望所有的bootstrapTasks项目(对bootstrapper.BootstrapAsync()的调用一次启动,并让UI线程接收所有项目的进度事件。当整个批次完成时,我也需要处理它。

更新1

尝试使用Task.Run()修复了UI锁定问题,但只执行第一个Task实例。更新foreach循环:

foreach (var targetMachine in vm.TargetMachines)
{
    var tm = targetMachine; // copy closure variable
    var bootstrapContext = new BootstrapContext(tm.MachineName, true)
    {
        AdminUser = vm.AdminUser,
        AdminPassword = vm.AdminPassword
    };
    var bootstrapper = new AgentBootstrapper(bootstrapContext);
    Debug.WriteLine("Starting Bootstrap task on default thread pool...");
    var task = Task.Run(() =>
    {
        var pid = bootstrapper.Bootstrap(bootstrapContext, progress);
        return pid;
    });
    Debug.WriteLine("Adding Task<int> " + task.Id + " to List<Task<int>>.");
    tasks.Add(task);
    await Task.WhenAll(tasks);  // Don't proceed with the rest of this function untill all tasks are complete
}

更新2

await Task.WhenAll(tasks);移动到foreach循环之外可以使所有任务并行运行。

async/await生成的代码中没有任何内容涉及线程的创建。使用async关键字不会导致使用其他线程。async所做的只是允许您使用await关键字。如果您希望在另一个线程上发生某些事情,请尝试使用Task.Run

在线程池上运行任务(使用默认的任务调度程序),并在UI线程中运行它们上的await Task.WhenAll(bootstrapTasks)

最新更新