UWP Background Task Operation Error

0

I am programming for UWP with C# language. I am reading about background task and create and register an in-process background task.
The registration code of background Task:

public static async Task RegisterBackgroundTask()
    {
        //
        // Check for existing registrations of this background task.
        //
        foreach (var cur in BackgroundTaskRegistration.AllTasks.Values)
        {
            if (cur.Name == taskName) // The task is already registered.
                return;
        }

        //
        // Universal Windows apps must call RequestAccessAsync before registering any of the background trigger types.
        // To ensure that your Universal Windows app continues to run properly after you release an update,
        // you must call RemoveAccess and then call RequestAccessAsync when your app launches after being updated.  
        //    
        var requestAccess = await BackgroundExecutionManager.RequestAccessAsync();
        if (requestAccess == BackgroundAccessStatus.DeniedByUser ||
            requestAccess == BackgroundAccessStatus.DeniedBySystemPolicy)
            return;

        //
        // Register the background task.
        //
        var builder = new BackgroundTaskBuilder();

        builder.Name = taskName;
        builder.SetTrigger(new TimeTrigger(15, false));
        //builder.AddCondition(condition);

        var task = builder.Register();

    }

my OnBackgroundActivated in App.cs:

    volatile bool _cancelRequested = false;
    BackgroundTaskDeferral _deferral;
protected override async void OnBackgroundActivated(BackgroundActivatedEventArgs args)
    {
        base.OnBackgroundActivated(args);

        // Query BackgroundWorkCost
        // Guidance: If BackgroundWorkCost is high, then perform only the minimum amount
        // of work in the background task and return immediately.
        var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;
        if (cost == BackgroundWorkCostValue.High)
            return;

        args.TaskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

        _deferral = args.TaskInstance.GetDeferral();

        if (!_cancelRequested) // start one or more asynchronous methods using the await keyword
            await BackgroundTaskExecution.UpdateTile();

        _deferral.Complete();
    }

    private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
    {
        _cancelRequested = true;
    }

These are my codes. I run this program and wait for 15 minutes. My tile updated. I wait 15 minutes again. My Tile updated again. I run app and test my bacground task with Visual Studio 2017 Lifecycle Events toolbar. It's work good.
So there is no problem in my code. But if I close my app after deploy it and wait for 15 minutes again, my tile don't update anymore. I run app again with Visual Studio and use Lifecycle Events toolbar for force backgroundTask to run. I always get this error: (I mention that this is happen if I close my app after deploy but before that, it's work itself or with toolbar)

Error

after click OK there is no more background task for my app and I have to register it again. I get same error again after register it and close app and run app again. I don't know why this happen. Is it possible to help me?

Update 1:
I changed my code today but I face new problem today. I run BackgroundTaskExecution.UpdateTile() in my background task code. BackgroundTaskExecution is a class in my CommonClass library that I created for my project. I move that class to my main project. now my project work goods. my background task run after deploy my app again. It's work when I am in my app or outside of app. but there is new problem. I run background task with time or lifecycle toolbar when my app is running, it's work without error but when I wait for background task to run while my app is not running, I get error in my background task. (I add try-catch block and I get exception message when my app is not running and show it on tile) the error message is:

The application called an interface that was marshalled for a different thread (Exception from HRESULT: 0x8001010E) (RPC_E_WRONG_THREAD)

so this is my new question. Why my problem solved with move my class to my main project? Why I get this error when I use in-app background task?

Update 2: I delete all unnecessary code. I write an app that you click on the button. background task registered and you can update tile with change Time zone. It's work until my app is open. If I close my app, then it doesn't work anymore. Please help me. Link Download Project

Thanks and I'm sorry for English type.

c#
uwp
asked on Stack Overflow Aug 26, 2017 by Amin Borjian • edited Sep 1, 2017 by Amin Borjian

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0