简体   繁体   中英

Windows Phone 8.1 - Exception when creating WriteableBitmap from a Background Task

I'm building a Windows Phone 8.1 app (Windows Runtime, not Windows Phone Silverlight 8.1). I have created a Background Task that I trigger by using a Maintenance Trigger. Inside the background task, I need to create a WriteableBitmap from one picture of Camera Roll. My code is as follows:

public sealed class Class1 : IBackgroundTask
{
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        var files = await KnownFolders.CameraRoll.GetFilesAsync();

        ShowNotification("Process has started");

        using(var fileStream = await files[0].OpenStreamForReadAsync())
        {
            WriteableBitmap writeableBitmap = await BitmapFactory.New(1, 1).FromStream(fileStream.AsRandomAccessStream());
        }

        ShowNotification("Process has ended");

        deferral.Complete();
    }
}

When I run the background task, the two notifications work as expected, but I'm getting the following exceptions:

"A first chance of exception of type "System.Exception" occurred in WriteableBitmapEx.WinRT.DLL""
"A first chance of exception of type "System.Exception" occurred in mscorlib.dll"

"The proccess has ended with code 1 (0x1)"

If I remove this lines:

using(var fileStream = await files[0].OpenStreamForReadAsync())
{
    WriteableBitmap writeableBitmap = await BitmapFactory.New(1, 1).FromStream(fileStream.AsRandomAccessStream());
}

Everything works as expected, the 2 notifications appear and no exception is thrown.

Any ideas?

Thank you.

Use XamlRenderingBackgroundTask in order to generate XAML graphics in the background. Note that it is recommended you use C++ for this in order to minimize memory usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM