locked
BackgroundDownloader, Toast, and Multiple Files

    Question

  • My app needs to download multiple files, and I want to notify the user when everything has been downloaded. (These are video files containing training material. If the user decides to download a whole course, it will contain multiple modules, each of which is broken down into clips. So we might be fetching a couple of hours of content, split into clips each of which contains between 1 and 10 minutes of material.)

    As far as I can tell, the documentation doesn't provide any explicit guidance on how to do this. I've found a way that looks like it might work, but I'm not entirely sure whether what I'm doing is supported. I'm seeing a slightly strange issue.

    What I'm trying is this: I'm creating a single BackgroundDownloader, with its SuccessToastNotification set to a ToastNotification object. I then call CreateDownload multiple times (twice in my experiment, but it will be more with the real app). And only after I've created all the downloads I'm going to want do I call StartAsync on all of them. It appears that if you try to make any further calls to CreateDownload after you've called StartAsync on at least one item, you get an exception, telling you that if you've enabled toast, you're not allowed to do that. At least, I think that's what the exception's telling me - the wording's not totally clear:

    A method was called at an unexpected time.
    
    At least one operation created by this object has already started. When
    configuring toast/tile notifications, operation can only be created until
    one of them is started.

    That's annoying, but it's not the problem.

    The issue is that if I run my app, start the downloads, then switch away from the app (e.g. back to the desktop), my app doesn't seem to be suspended, like you'd normally expect a store app to be in that situation. Instead, Task Manager's Details view shows it as "Not responding". The download seems to progress, judging by the network section of Windows Resource Monitor. But when the download completes, I don't see the toast initially. If I switch back to my app that the toast appears at that point! Or, if I just wait for about 5 minutes, the toast does eventually appear.

    It's possible that this problem has nothing to do with multiple downloads or use of toast, of course - there may be something else I'm doing wrong. Is there anything special you're supposed to do to enable suspend to work when doing background transfers?

    Even if I can fix the current issue, there's a problem with this technique:

    It seems that once I've kicked off the downloads, I can't change my mind about what's in a group of files for which there is a single toast notification. This is a bit annoying, because my app allows users to pick individual sections from a course - we don't require them to download the whole thing - and I'd prefer to start downloading any single section as soon as they choose it, and then let them add more. But I'd prefer to provide course-level notifications. (Or, ideally, give users the option to ask to be notified only once everything they selected has finished downloading.)

    But from the look of it, that's not going to be possible - you're not allowed to add new items to an existing BackgroundDownload once it's in progress if it has toast enabled. And you're also not allowed to change your mind about whether a group shows toast notifications after you've kicked it off. (The only way we could make it work is to have some sort of 'commit' stage where the user tells us to start the download after they've selected content. But I don't really like that UX - it makes it too easy to think you've asked to download a load of stuff, only to find later that the app never started the download.)

    (What I really want is to be able to run code when a download completes and decide whether to show a notification at that point, but as far as I can tell, there's no way to get Windows to run your code when downloads complete unless you happen to be running already at the time. I'd love to use completion of background transfer as a trigger for a background task, but that doesn't appear to be supported.)

    One other possible hack had occurred to me: start a dummy download that fetches some small piece of content that we don't need, set its priority to be lower than all the other downloads, and attach toast to that. This would give us a way to do the "Everything has finished downloading now" alert. But I'm not sure about that - if Windows supports multiple concurrent downloads, then it might well finish our dummy before it has finished downloading the last of the real files.

    Is there a better way to do what I'm trying to do here?

    Tuesday, June 17, 2014 4:17 PM

All replies

  • By the way, I'm seeing exactly the same problem with the SDK sample - if I download the C# project from http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61#content and run Scenario 3 I also see:

    • Instead of being suspended, Task Manager reports my process as "not responding" when I switch away
    • Toast notifications are delayed either until I return to the app, or for about 5 minutes

    So apparently it's not just my code - the example code (which also attached multiple downloads to a single BackgroundDownloader, like I did) seems to have exactly the same issues.

    Wednesday, June 18, 2014 7:53 AM
  • Same thing happens here, anyone has comment?

    Friday, June 20, 2014 6:35 AM