I'm trying to set an image to lockscreen or wallpaper in my C# W10 UWP app in a BackgroundTask
... I can do this just fine in normal execution, but when I put the same code into a BackgroundTask
, the code hangs at StorageFile.CreateStreamedFileFromUriAsync
.
// See if file exists already, if so, use it, else download it
StorageFile file = null;
try {
file = await ApplicationData.Current.LocalFolder.GetFileAsync(name);
} catch (FileNotFoundException) {
if (file == null) {
Debug.WriteLine("Existing file not found... Downloading from web");
file = await StorageFile.CreateStreamedFileFromUriAsync(name, uri, RandomAccessStreamReference.CreateFromUri(uri)); // hangs here!!
file = await file.CopyAsync(ApplicationData.Current.LocalFolder);
}
}
// Last fail-safe
if (file == null) {
Debug.WriteLine("File was null -- error finding or downloading file...");
} else {
// Now set image as wallpaper
await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(file);
}
Are there any restrictions to StorageFile
in BackgroundTasks
that I'm unaware of? Some googling yielded no such restrictions...
Any ideas?
Thanks.