Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add background task to Windows Store App

using c#, VS 2013

Try to add some background task to my Store app (idea to show toast if some data in my Json file contains current date and time).

What was done:

1.Create Windows Runtime Component, that implement IBackgroundTask , add reference to my Windows Store App. Inside WRC create class that contains next code :

namespace BackgroundTask
{
public sealed class EventChecker: IBackgroundTask
{
    ThreadPoolTimer  _periodicTimer = null;
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        _periodicTimer 
            = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(PeriodicTimerCallback), TimeSpan.FromSeconds(30));
    }

    private void PeriodicTimerCallback(ThreadPoolTimer timer)
    {
        CheckEventAndShowToast();
    }
     ....
}

2.Register task : In MainPage.xaml.cs add in method OnNavigatedTo registering of this background task. Code:

        protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        navigationHelper.OnNavigatedTo(e);

        var taskName = "EventCheckerTask";
        if (BackgroundTaskRegistration.AllTasks.Count > 0)
        {
            foreach (var cur in BackgroundTaskRegistration.AllTasks)
            {
                if (cur.Value.Name != taskName)
                {
                    BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                    builder.Name = taskName;
                    builder.TaskEntryPoint = "BackgroundTask.EventChecker";
                    BackgroundTaskRegistration taskToRegister = builder.Register();
                }
            }
        }
        else
        {
            BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
            builder.Name = taskName;
            builder.TaskEntryPoint = "BackgroundTask.EventChecker";
            BackgroundTaskRegistration taskToRegister = builder.Register();
        }
    }

Use MSDN1, MSDN2, MSDN3 links.

Also OnComplete not implement - because i don't need it (or I must to implement it anyway?)

3.Declare in manifest.

Set toast capable to "YES":

enter image description here

Declare background Task:

enter image description here

4.Check functionality of all method that i want to use for background - all Ok and work

Durring debugging all it's ok, no errors/ exceptions, but nothing happend. Try to debug step by step - looks like all it's ok, think i make some mistake in code.

So question: where i'm wrong, why i cant launch my background task that must to check data and do required action if some conditions are as required?

EDIT


Part 2 - Try to realize background task in new solution.

What was done: Create new simple CRC :

namespace Tasks
{
public sealed class Tasks : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        //for checking place debug point
       //TODO something 
    }
}
}

Also in main.xaml.cs placed next code:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {           
        CheckTaskRegistration();
    }
    private void CheckTaskRegistration()
    {
        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == "Tasks")
            {
                isTaskRegistered = true;
                break;
            }
        }
        if (!isTaskRegistered)
        {
            RegisterBackgroundTask2("Tasks", "Tasks.Tasks");
        }
    }

    private void RegisterBackgroundTask2(string name, string entrypoint)
    {
        BackgroundTaskBuilder btb = new BackgroundTaskBuilder();
        btb.Name = name;
        btb.TaskEntryPoint = entrypoint;
       //IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(1, false);
       // btb.SetTrigger(everyMinuteTrigger);
        btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
        BackgroundTaskRegistration task = btb.Register();
    }

As result got, that with this trigger btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false)); all works - i can go inside Run method, but if I try to use TimeTrigger like

       //IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(1, false);
       // btb.SetTrigger(everyMinuteTrigger);

nothing happend - wait few minutes try few times (placed instead prev trigger registration).

Question - Why? Do i must to do something more? Also old questions are without answers too...

Also try to use it with my App - all worksperfect, but only if i connect to lan... But why it's not work with time trigger?

like image 393
hbk Avatar asked Mar 27 '14 20:03

hbk


People also ask

How do I force an app to run in the background windows?

Select Start , then select Settings > Privacy > Background apps. Under Background Apps, make sure Let apps run in the background is turned On.

What are background tasks in Windows?

Background tasks are one method on Windows 10 for running code in the background. They are part of the standard application platform and essentially provide an app with the ability to register for a system event (trigger) and when that event occurs run a predefined block of code in the background.

How do I see my background tasks?

To open the Background Task Inspector, select View > Tool Windows > App Inspection from the menu bar. With the Background Task Inspector tab selected in the App Inspection window, I run the app again on a device or emulator running API level 26 or above.


1 Answers

A spend a little bit more time and found few root causes for my problem:

  1. I must to use some trigger with my BackgroundTask if I want to use it and launch. problem here that ther is not exactly what i need exist (or maybe i need to read a little bit more about triggers). So if I add some trigger, BackgroundTask can be launched after such event happend. Example:

    //Time trigger
    IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(15, false);
    btb.SetTrigger(everyMinuteTrigger);
    
    //one of the standart tirgger
    btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
    
  2. If I want to use TimeTrigger, in EDIT i write code with TimeTrigger(1, false);, but after reading some more detailed documentation found "that the time trigger only accepts values greater than or equal to 15; smaller values fail during Register." MSDN

  3. Also if I want to use TimeTrigger i must to add LockScreenNotification support to my App

Currently I can launch backgroundTask every 15 min, but it's not exactly what i want... So, regarding this post quation - i found answer, but still need to read more deeply about BackgroundTask

like image 99
hbk Avatar answered Sep 24 '22 01:09

hbk