Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve that AttachAsync of a DownloadOperation does not return immediately?

When using the Background Transfer API we must iterate through current data transfers to start them again ahen the App restarts after a termination (i.e. system shutdown). To get progress information and to be able to cancel the data transfers they must be attached using AttachAsync.

My problem is that AttachAsync only returns when the data transfer is finished. That makes sense in some scenarios. But when having multiple data transfers the next transfer in the list would not be started until the currently attached is finished. My solution to this problem was to handle the Task that AttachAsync().AsTask() returns in the classic way (not use await but continuations):

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());
}

It kind of works (in the actual code I add the downloads to a ListBox). The loop iterates through all downloads and executes StartAsync. But the downloads are not really started all at the same time. Only one is runninng at a time and only if it finishes the next one continues.

Any solution for this problem?

like image 684
Jürgen Bayer Avatar asked Dec 19 '12 00:12

Jürgen Bayer


1 Answers

The whole point of Task is to allow you to have the option of parallel operations. If you await then you are telling the code to serialize the operations; if you don't await, then you are telling the code to parallelize.

What you can do is add each download task to a list, telling the code to parallelize. You can then wait for tasks to finish, one by one.

How about something like:

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....
        }
    }
}
like image 187
chue x Avatar answered Nov 01 '22 02:11

chue x