windows phone 8.1 execute backgroundtask from app (timertrigger)

534 views Asked by At

I have an app with a backgroundtask. This task is triggered by the timertrigger. When I register it, it executes after the timer is triggered (each hour).

But i like to execute the task "at will" too. Can my app send a trigger to the backgroundtask, so the task gets executed before the timer expires? Or could I invoke the Run method from the backgroundtask? How would I do that?

Example code would be appreciated. This is my Run method from the backgroundtask.

public async void Run(IBackgroundTaskInstance taskInstance)
{
    _deferral = taskInstance.GetDeferral();
    if (!_listening)
    {
        taskInstance.Canceled += taskInstance_Canceled;
        await ... // background logic goes here...
    }
    else
    {
        _deferral.Complete();
    }

}

EDIT

I agree with @Fred to move the logic to a shared project, communicating background/foreground with an isolated storage.

The issue is this: On a Microsoft Band I've created a tile. The tile contains messages with buttons. When such a button is pressed an event is raised. To catch the events you have to instantiate an IBandClient. The problem with background tasks is that the OS can kill your task. When it does, tile events are no longer handled so I would like to restart it per user request.

So the shared project is definitely a way to "inform" my foreground the backgrountask listener is still active.

But the question remains: how to start the background task "with a click of a button"?

1

There are 1 answers

1
Fred On

I think you're going for the wrong approach. You shouldn't be in the position to execute your background task manually. If your background task and your main project need to execute the same methods they should use a shared project.

  • Create a shared project. Put the logic inside a class there.
  • Reference that shared project both in your main project and in your background task
  • Whenever your background task executes, it actually instantiates and calls the class from the shared project. Your background task now only becomes the wrapper for the execution and would refactor to this:

    public async void Run(IBackgroundTaskInstance taskInstance)    
    {
        _deferral = taskInstance.GetDeferral();
        if (!_listening)
        {
            taskInstance.Canceled += taskInstance_Canceled;
            SharedProject.MyLogicHelper mlh = new SharedProject.MyLogicHelper();
            await mlh.DoFancyStuff();
        }
        else
        {
            _deferral.Complete();
        }   
    
    }
    

    Now if you want to call that task manually from the running foregound app, just execute that code from your ViewModel:

    SharedProject.MyLogicHelper mlh = new SharedProject.MyLogicHelper();
    await mlh.DoFancyStuff();
    

You can use the IsolatedStorage to communicate between the foreground and the background task (if you want to set flags or prevent the code to be executed only after a given interval).