如何解决DownloadOperation的AttachAsync不会立即返回?

Jür*_*yer 10 windows-runtime windows-store-apps

当使用Background Transfer API时,我们必须迭代当前数据传输以在应用程序重新启动(即系统关闭)后重新启动它们.要获取进度信息并能够取消数据传输,必须使用AttachAsync附加它们.

我的问题是AttachAsync仅在数据传输完成时返回.这在某些情况下是有道理的.但是,当进行多次数据传输时,列表中的下一次传输将不会在当前连接完成之前启动.我对这个问题的解决方案是处理AttachAsync()的任务.AsTask()以经典的方式返回(不使用await而是continuation):

IReadOnlyList<DownloadOperation> currentDownloads =
   await BackgroundDownloader.GetCurrentDownloadsAsync();
foreach (var downloadOperation in currentDownloads)
{
   Task task = downloadOperation.AttachAsync().AsTask();

   DownloadOperation operation = downloadOperation;
   task.ContinueWith(_ =>
   {
      // Handle success
      ...
   }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion,
   TaskScheduler.FromCurrentSynchronizationContext());

   task.ContinueWith(_ =>
   {
      // Handle cancellation
      ...
  }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled,
  TaskScheduler.FromCurrentSynchronizationContext());

  task.ContinueWith(t =>
  {
     // Handle errors
      ...

  }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted,
  TaskScheduler.FromCurrentSynchronizationContext());
}
Run Code Online (Sandbox Code Playgroud)

它有点工作(在实际的代码中我将下载添加到ListBox).循环遍历所有下载并执行StartAsync.但下载并非真正同时启动.每次只有一个运行,只有当它完成后,下一个继续运行.

解决这个问题的任何方法?

chu*_*e x 3

重点Task是让您可以选择并行操作。如果你await那么你是在告诉代码序列化操作;如果你不等待,那么你就是在告诉代码并行化。

您可以做的是将每个下载任务添加到列表中,告诉代码并行化。然后您可以等待任务一项一项完成。

怎么样:

IReadOnlyList<DownloadOperation> currentDownloads = 
    await BackgroundDownloader.GetCurrentDownloadsAsync();
if (currentDownloads.Count > 0)
{
    List<Task<DownloadOperation>> tasks = new List<Task<DownloadOperation>>();
    foreach (DownloadOperation downloadOperation in currentDownloads)
    {
        // Attach progress and completion handlers without waiting for completion
        tasks.Add(downloadOperation.AttachAsync().AsTask());
    }

    while (tasks.Count > 0)
    {
        // wait for ANY download task to finish
        Task<DownloadOperation> task = await Task.WhenAny<DownloadOperation>(tasks);
        tasks.Remove(task);

        // process the completed task...
        if (task.IsCanceled)
        {
            // handle cancel
        }
        else if (task.IsFaulted)
        {
            // handle exception
        }
        else if (task.IsCompleted)
        {
            DownloadOperation dl = task.Result;
            // handle completion (e.g. add to your listbox)
        }
        else
        {
            // should never get here....
        }
    }
}
Run Code Online (Sandbox Code Playgroud)