How to determine the time at which a background task will be performed

Asked

Viewed 111 times

1

I am programming for Windows 10, and at a certain time in a day I want my background task to be faced.

Here is my registration code:

on the home screen:

var trigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false);
            var condition = new SystemCondition(SystemConditionType.InternetAvailable);
            var tarefa = RegistrarTarefasSegundoPlanoAsync.RegisterBackgroundTask(typeof(SalvaImagemTask).FullName, "SalvaImagemTask", trigger, condition);

Registerbackgroundtask.Cs

public static BackgroundTaskRegistration RegisterBackgroundTask(
                                                string taskEntryPoint,
                                                string name,
                                                IBackgroundTrigger trigger,
                                                IBackgroundCondition condition)
        {

            foreach (var cur in BackgroundTaskRegistration.AllTasks)
            {
                if (cur.Value.Name == name)
                {
                    return (BackgroundTaskRegistration)(cur.Value);
                }
            }


            var builder = new BackgroundTaskBuilder();

            builder.Name = name;
            builder.TaskEntryPoint = taskEntryPoint;
            builder.SetTrigger(trigger);

            if (condition != null)
            {

                builder.AddCondition(condition);
            }

            BackgroundTaskRegistration task = builder.Register();

            return task;
        }

here is how it is registered in my manifest

Package.appxmanifest

  • I believe you can use the Time class. https://msdn.microsoft.com/pt-br/library/system.timers.timer(v=vs.110). aspx

1 answer

0


It is not possible to determine a specific time for a background task to be executed. You can use a timer Rigger to specify the time interval between background task executions, respecting the minimum time of 15 min, is a rule. Below is an example of using the Trigger timer:

                BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
                taskBuilder.Name = nameof(BackgroundClassName);
                taskBuilder.TaskEntryPoint = typeof(BackgroundClassName).ToString();
                // min 15 minutes
                taskBuilder.SetTrigger(new TimeTrigger(15, false));
                BackgroundTaskRegistration registration = taskBuilder.Register();

If you want to see other triggers and how to use background tasks efficiently, visit this MVA in Portuguese (https://mva.microsoft.com/pt-br/training-courses/live-tiles-e-background-execution-14433?l=K9L5uVmkB_1605192797)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.