The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[UWP] ApplicationTrigger.RequestAsync in OnSuspending closes app (W10M) RRS feed

  • Question

  • Hi,

    I'm developing an app that needs to start a BackgroundTask in it's Suspending event. The BackgroundTask is used for updating the live tile - the update process may take more than 30 seconds, therefore (unfortunately) using a TimeTrigger is not an option. While everything works great on W10 Desktop (with an ApplicationTrigger), the application is closed and removed from the task manager in Windows 10 Mobile. This is quite annoying if you only wanted to switch between apps.

    Is there a way to make a TimeTrigger work on W10M even if it takes more than 30s to complete? Or is there an alternative?

    MaintenanceTrigger is no option, because phones or laptops are usually not plugged to AC power and therefore the BackgroundTask is not executed (MaintenanceTrigger API).

    I would really like to use the TimeTrigger or an alternative, but ApplicationTrigger seems to be the only Trigger matching the most important part of my requirements (long running BackgroundTask which is frequently executed - for example every 30 minutes). How can I prevent the app from closing when ApplicationTrigger.RequestAsync() is called in the Suspending event?

    One last thing: If I request the ApplicationTrigger by clicking an a button in the app, the app is not be closed.

    Here is the code of my Suspending-EventHandler:

    private async void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        await UpdateLiveTiles();                    
        deferral.Complete();
    }
    
    /// <summary>
    /// Updates the live tile
    /// </summary>
    public async Task<bool> UpdateLiveTiles()
    {
        if (liveTileApplicationTrigger == null) return false;
    
        await liveTileApplicationTrigger.RequestAsync();
        return true;
    }



    • Edited by Manuel Kurtz Saturday, October 28, 2017 5:33 PM fixed some issues
    Saturday, October 28, 2017 5:30 PM

Answers

  • Hi,

    I could finally resolve the issue by myself. In my source code I first got the pixel data of an image using this code:

    using (var imageFileStream = await imageFile.OpenAsync(FileAccessMode.Read))
                {
                    BitmapDecoder imageDecoder = await BitmapDecoder.CreateAsync(imageFileStream);
                    width = imageDecoder.PixelWidth;
                    height = imageDecoder.PixelHeight;
                    pixels =
                        (
                            await imageDecoder.GetPixelDataAsync
                            (
                                BitmapPixelFormat.Rgba8,
                                BitmapAlphaMode.Straight,
                                getBitmapTransformUniformToFill(width, height, width, height),
                                ExifOrientationMode.IgnoreExifOrientation,
                                ColorManagementMode.ColorManageToSRgb
                            )
                        )
                        .DetachPixelData();
                }

    This works quite fine, but after getting the pixel data with this code I need to manipulate the image file:

    using (var chartFileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    BitmapEncoder imageEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, chartFileStream);
                    imageEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, width, height, 96.0, 96.0, pixels);
                    await imageEncoder.FlushAsync();
                }

    Writing to the same file the pixel data (pixels) has been obtained from causes the app to close on Windows Mobile. The issue could be resolved by creating a new file which is used in the second code snippet. 

    I don't know why using the same file causes the issue - I thought that wrapping file read/write operations in a using-statement should prevent such issues.

    I hope this helps other people facing the same issue

    Best regards,

    Manuel


    Manuel Kurtz

    • Marked as answer by Manuel Kurtz Friday, February 2, 2018 3:12 PM
    Friday, February 2, 2018 3:12 PM

All replies

  • Hi Manuel Kurtz,

    Did you get some error info when your app closed? From you above description and code, I can not find why this issue happens, could you provide a simple sample to help me look into it?

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 31, 2017 10:14 AM
  • Hi Breeze,

    Unfortunately, I do not get any error messages. I've got a global error handling mechanism that writes errors to a database. 

    I'm currently working on a smaller app which does not show the issue yet. I am suspecting that the issue might occur, because I am updating images in the application's folder which might be in use for the current live tiles. But that should throw an exception if this was the issue, shouldn't it?

    I hope I'm able to reproduce the issue in the smaller app to provide it as example. If that is not possible, I'll provide access to my app and sample data with which it is possible to reproduce the issue.

    Debugging the issue is quite difficult, because the issue only occurs if Visual Studio is not debugging the app. Is it possible to configure VS so that the app works as if I was not debugging it?

    I'm looking forward to your answers and I'll try to provide a sample app by the end of the week.

    Kind regards,

    Manuel


    Manuel Kurtz

    Wednesday, November 1, 2017 3:51 PM
  • Hi again,

    I've prepared a minimized sample (CarStatistics_BackgroundTasks_Sample.zip) of my app. It can be downloaded here: 

    Sample app for BackgroundTask

    The zip archive contains the solution, required import data for the app and a _readme-file for further processing. I hope that this sample helps finding the issue.

    As mentioned before, when executing the BackgroundTask in the Suspending event, the application is closed (but this should not be).

    Kind regards,

    Manuel


    Manuel Kurtz

    Friday, November 3, 2017 8:32 PM
  • Hi Manuel Kurtz,

    Thanks for sharing the sample, I can see the issue in my side and we are trying to investigate it, appreciate your patience.

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 7, 2017 6:37 AM
  • Hi Breeze,

    I've found out, that it could have to do sth. with generating the image for the live tile. Here is an example image:

     

    When only setting an image as tile image, the application is not closed. This can be tested with CarStatistics from the Store: CarStatistics - only go into settings/other and select "images of the cars". Therefore the issue occurs in LiveTile.cs in the following function:

    private static async Task<List<LiveTileImage>> getStatisticImages(CarStatisticsDataContext db)

    Both statistic types (bar chart, line chart) cause the issue and both create new files in the application's folder - could this be a reason for closing the application?

    I will test some changes in this function hoping I'll find something.

    Kind regards,

    Manuel 


    Manuel Kurtz


    Thursday, November 9, 2017 5:49 PM
  • Hi,

    I could finally resolve the issue by myself. In my source code I first got the pixel data of an image using this code:

    using (var imageFileStream = await imageFile.OpenAsync(FileAccessMode.Read))
                {
                    BitmapDecoder imageDecoder = await BitmapDecoder.CreateAsync(imageFileStream);
                    width = imageDecoder.PixelWidth;
                    height = imageDecoder.PixelHeight;
                    pixels =
                        (
                            await imageDecoder.GetPixelDataAsync
                            (
                                BitmapPixelFormat.Rgba8,
                                BitmapAlphaMode.Straight,
                                getBitmapTransformUniformToFill(width, height, width, height),
                                ExifOrientationMode.IgnoreExifOrientation,
                                ColorManagementMode.ColorManageToSRgb
                            )
                        )
                        .DetachPixelData();
                }

    This works quite fine, but after getting the pixel data with this code I need to manipulate the image file:

    using (var chartFileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    BitmapEncoder imageEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, chartFileStream);
                    imageEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, width, height, 96.0, 96.0, pixels);
                    await imageEncoder.FlushAsync();
                }

    Writing to the same file the pixel data (pixels) has been obtained from causes the app to close on Windows Mobile. The issue could be resolved by creating a new file which is used in the second code snippet. 

    I don't know why using the same file causes the issue - I thought that wrapping file read/write operations in a using-statement should prevent such issues.

    I hope this helps other people facing the same issue

    Best regards,

    Manuel


    Manuel Kurtz

    • Marked as answer by Manuel Kurtz Friday, February 2, 2018 3:12 PM
    Friday, February 2, 2018 3:12 PM