Question

Without going into too much detail, here's the basic scenario.

I have a Metro App that downloads files off a server.

Once the files are downloaded, a ListView is updated in the UI with it's basic properties. If the user selects a ListViewItem from the ListView, it's StorageFile contents are displayed next to it. The user then takes some action based on the selected StorageFile content.

Now for the design question:

On Suspending or Shutdown - I would like the app to continue downloading files off the server, so I register for 2 BackgroundTasks. One is a SystemTrigger, which kicks off immediately and the other is a TimeTrigger that kicks off every 15 minutes. The BackgroundTasks also raise notifications, so that the user is aware that more files are available.

I Register these tasks when the Suspending event is raised (and on Shutdown). I Unregister these tasks when the Resuming event is raised or when the App is constructed from scratch. This ensures that either the UI or BackgroundTask is responsible for processing, and not both.

When testing this in Visual Studio, the BackgroundTasks work as expected. However, outside of Visual Studio the BackgroundTasks never seem to trigger. These BackgroundTasks request Lock Screen access, via BackgroundExecutionManager.RequestAccessAsync().

Is there a fundamental flaw in this design or am I missing something really simple? Any help would be appreciated.

Était-ce utile?

La solution

There is a fundamental flaw in this design:

I Register these tasks when the Suspending event is raised (and on Shutdown). I Unregister these tasks when the Resuming event is raised.

This is not how BackgroudnTasks should be used. You should register your tasks only once and keep them registered, and once the task is started, check if application is running (using shared property in ApplicationSettings). If yes, just end the task, if not, to the downloading. Also note that background task will run only for about two seconds each 15 minutes. You cannot just download large data in these tasks, only check for new files, start BackgroundDownloader and/or show Toast notification.

When testing this in Visual Studio, the BackgroundTasks work as expected. However, outside of Visual Studio the BackgroundTasks never seem to trigger. These BackgroundTasks request Lock Screen access, via BackgroundExecutionManager.RequestAccessAsync().

First of all, are these tasks in separate library/project which is Windows Runtime Component? If not, then it won't work. Second, if your task requires lockscreen access, user must allow this execution in a dialog shown after the RequestAccessAsync call - this just won't work if you're registering the task in Suspending event.

Anyway, as I mentioned, for downloading large files you should use these classes:

StorageFile file = await folder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(new Uri(filePath), file);

Let me know, if this helps or you need any more hints.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top