1

In my windows store app I am using BackgroundDownloader class to handle multiple background downloads. After all my 3 downloads are 100% complete, I close and open application, then when I run BackgroundDownloader.GetCurrentDownloadsAsync, it returns all downloads with 100% completed state.

IReadOnlyList<DownloadOperation> readOnlyList = await BackgroundDownloader.GetCurrentDownloadsAsync();

My expectation is completed downloads will be removed from list or somehow I need to force-remove them. But could not find any function for it.

How can I remove completed downloads from CurrentDownloads list?

-Side question: Will GetCurrentDownloadsAsync function return all active downloads (including other applications downloads) or only downloads are performing in my application?

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158

1 Answers1

6

You have to execute the completion handler by doing AttachAsync() on the downloads that just completed. After that, downloads will not appear anymore in the results of GetCurrentDownloadsAsync().

Try:

private async void Foo()
{
    var downloads = await BackgroundDownloader.GetCurrentDownloadsAsync();
    foreach (var download in downloads)
    {
        var task = download.AttachAsync().AsTask();
        var notAwait = task.ContinueWith(OnCompleted);
    }
}

private void OnCompleted(Task<DownloadOperation> task)
{
    DownloadOperation download = task.Result;
    // ...
}
kiewic
  • 15,852
  • 13
  • 78
  • 101
  • 1
    This is working like a charm. I believe lots of up-votes will rain here because there is no valid answer on Google results. Thanks! – Teoman shipahi Jan 13 '15 at 05:44
  • This solution isn't working with me for some reason, After the download completed, if another request to the same uri called, It throws exception Bad request 400 even if i called AttachAsync. The problem doesn't exist when I restart the application. Any Thoughts? – Ahmed Rashad Mohamed Jan 18 '15 at 13:09
  • I figured it out, I was reusing the same BackgroundDownloader, I fixed it by instantiating a new instance of BackgroundDownloader each time I start a new download. I don't know if there are other ways to fix my issue but this one worked. – Ahmed Rashad Mohamed Jan 18 '15 at 13:22
  • @Ahmed You may be setting headers in the `BackgroundDownloader` you do not need in both downloads. – kiewic Jan 18 '15 at 17:25