locked
Can I register background task for several system events (NetworkStateChange and TimeTrigger)?

    Question

  • Could you please advise how to register one task for two triggers?

    I need to capture NetworkStateChange system trigger type and also call "Run()" function in a interval time.

    I'm trying do to something like this:

    public static IBackgroundTaskRegistration register(string taskEntrypoint, string taskName)
    {
    	//get already registered task
    	IBackgroundTaskRegistration task = registeredTask(taskName);
    
    	//if task is registered then return it
    	if (task != null) return task;
    
    	BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
    	builder.Name = taskName;
    	builder.TaskEntryPoint = taskEntrypoint;
    
    	IBackgroundTrigger trigger;
    
    	//time trigger
    	trigger = new TimeTrigger(15, false);
    	builder.SetTrigger(trigger);
    
    	trigger = new SystemTrigger(Windows.ApplicationModel.Background.SystemTriggerType.NetworkStateChange, false);
    	builder.SetTrigger(trigger);
    
    
    	task = (IBackgroundTaskRegistration)builder.Register();
    	return task;
    }
    

    Friday, March 07, 2014 11:43 PM

All replies

  • A task can respond only to one trigger type. You can set multiple tasks to handle multiple events.

    --Rob

    • Marked as answer by _dima Saturday, March 08, 2014 12:21 AM
    • Unmarked as answer by _dima Saturday, March 08, 2014 12:28 AM
    Saturday, March 08, 2014 12:15 AM
    Owner
  • Thank you.

    Could you please advise how to create timer trigger for the a small time interval (i.e., 1-2 seconds)?

    Now I see that TimerTrigger allows 15 mins interval minimum... It's not enough due to my requirements.

    Saturday, March 08, 2014 12:21 AM
  • And could you please clarify what means this code (from the "How to declare background tasks"):

    <Extension Category="windows.backgroundTasks" EntryPoint="Tasks.SampleBackgroundTask">
          <BackgroundTasks>
            <Task Type="systemEvent" />
            <Task Type="timer" />
          </BackgroundTasks>
        </Extension>

    This background task has a two type's notifications... Why is it declared if task can capture only one type of system notification?

    Saturday, March 08, 2014 12:28 AM
  • Sorry. Loose terminology. You can register a task for only one trigger at a time. You can register it multiple times for multiple triggers.

    See the sample you linked for details: it demonstrates this.

    --Rob

    Saturday, March 08, 2014 1:03 AM
    Owner
  • A time trigger every 2 seconds is very much not possible. Background tasks must be lightweight.

    What is your scenario? Why do you need this frequency?

    Saturday, March 08, 2014 1:13 AM
    Owner
  • A time trigger every 2 seconds is very much not possible. Background tasks must be lightweight.

    What is your scenario? Why do you need this frequency?

    I need to update tile picture, because its updating is not work correctly. This article describes this problem.
    Saturday, March 08, 2014 1:27 AM
  • Sorry. Loose terminology. You can register a task for only one trigger at a time. You can register it multiple times for multiple triggers.

    See the sample you linked for details: it demonstrates this.

    --Rob

    So... I can register the same task (one sealed class with Run() function)  for one trigger at a time. What about task name? I can use one name for each registration of task?

    For example:

     
    //registration the task for time trigger	
    BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
    	builder.Name = "TaskNumber1";
    	builder.TaskEntryPoint = "taskComponent.taskClass";
    
    	IBackgroundTrigger trigger = new TimeTrigger(15, false);
     	builder.SetTrigger(trigger);
            builder.Register();
    
    //registration the task for network changing trigger	
            builder = new BackgroundTaskBuilder();
    	builder.Name = "TaskNumber1";
    	builder.TaskEntryPoint = "taskComponent.taskClass";
    	trigger = new SystemTrigger(Windows.ApplicationModel.Background.SystemTriggerType.NetworkStateChange, false);
    	builder.Register(); 

    Or I can use defferent names for tasks?

    Saturday, March 08, 2014 1:37 AM
  • The problems in the other thread are because you're not updating the tiles correctly. I've already responded there. You should fix that code rather than trying to hack a very ugly workaround.

    Trying to update tiles every 2 seconds is also very fast. Your users will be hard pressed to keep up with the information feed.

    Saturday, March 08, 2014 2:15 AM
    Owner
  • Hi Rob,

    Thank you for your help, I'v re-implemented tile notifications, but I still have an issue with task registration. 

    Time trigger task doesn't work.

    It's some testing code:

    namespace rtComponent
    {
        public sealed class NotificationTask : IBackgroundTask
        {
            //Run()
            //it's called when event is occured
            public void Run(IBackgroundTaskInstance taskInstance)
            {
                try
                {
                    // Get a deferral, to prevent the task from closing prematurely 
                    // while asynchronous code is still running.
                    BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
    
                    //
                    //some actions
    
                    // Inform the system that the task is finished.
                    deferral.Complete();
                }
                catch (Exception ex) { }
            }
    
            public static void register()
            {
                try
                {
                     //registration the task for the networking changing trigger	
                     BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                     builder.Name = "TestNotificationTaskNumber1";
                     builder.TaskEntryPoint = "rtComponent.NotificationTask";
    
                     IBackgroundTrigger trigger = new SystemTrigger(Windows.ApplicationModel.Background.SystemTriggerType.NetworkStateChange, false);
                     builder.SetTrigger(trigger);
                     builder.Register();             
    
                     //registration the task for the time trigger	
                     builder = new BackgroundTaskBuilder();
                     builder.Name = "TestNotificationTaskNumber2";
                     builder.TaskEntryPoint = "rtComponent.NotificationTask";
    				
                     trigger = new TimeTrigger(15, false);
                     builder.SetTrigger(trigger);
                     builder.Register();
                }
                catch (Exception ex) {}
            }
    
        }
    }

    Networking task (NetworkStateChange trigger) works correctly, but timer task (TimeTrigger for 15 mins)  doesn't work absolutely.

    Are there any ideas what is problem?





    • Edited by _dima Tuesday, March 11, 2014 11:40 AM
    Tuesday, March 11, 2014 11:36 AM