private async void RunBackgroudTask(object sender, RoutedEventArgs e)
        {
            var valueSet = new ValueSet {
                { "CT", 1 }
            };
            ApplicationTriggerResult applicationTriggerResult = await _applicationTrigger.RequestAsync(valueSet);

            switch (applicationTriggerResult)
            {
            case ApplicationTriggerResult.Allowed:
                break;

            case ApplicationTriggerResult.CurrentlyRunning:
                break;

            case ApplicationTriggerResult.DisabledByPolicy:
                break;

            case ApplicationTriggerResult.UnknownError:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #2
0
        public static async Task LoadUnreadNotifications(bool reset = true, bool showToasts = true, BackgroundTaskDeferral deferral = null)
        {
            reset = reset || (_unreadResult == ApplicationTriggerResult.Allowed &&
                              _unreadResult == ApplicationTriggerResult.CurrentlyRunning);
            if (_unreadResult != ApplicationTriggerResult.CurrentlyRunning)
            {
                _unreadResult = await RunAppTriggerBackgroundTask("sync", "notifications", "online", "unread", "toast", true, reset);
            }

            deferral?.Complete();
        }
Exemple #3
0
        public static async Task LoadParticipatingNotifications(bool reset = true, BackgroundTaskDeferral deferral = null)
        {
            reset = reset || (_participatingResult == ApplicationTriggerResult.Allowed &&
                              _participatingResult == ApplicationTriggerResult.CurrentlyRunning);
            if (_participatingResult != ApplicationTriggerResult.CurrentlyRunning)
            {
                _participatingResult = await RunAppTriggerBackgroundTask("sync", "notifications", "online", "participating", "toast", true, reset);
            }

            deferral?.Complete();
        }
        private static async Task <bool> RegisterAllBackgroundTasksAsyncHelper(bool runAppTrigger)
        {
            // Check for background registration permissions.
            var status = await BackgroundExecutionManager.RequestAccessAsync();

            Debug.WriteLine("BackgroundExecutionManager.RequestAccessAsync returned status " + status);
            if (status == BackgroundAccessStatus.DeniedBySystemPolicy ||
                status == BackgroundAccessStatus.DeniedByUser)
            {
                Debug.WriteLine("Cannot register background tasks. TODO: Notify user?");
                return(false);
            }

            // TODO: Remove this after development
            //UnregisterAllBackgroundTasks();

            // Background task parameters.
            ApplicationTrigger appTrigger = new ApplicationTrigger();
            SystemTrigger      sessionConnectedTrigger = new SystemTrigger(SystemTriggerType.SessionConnected, false);
            SystemCondition    userPresentCondition    = new SystemCondition(SystemConditionType.UserPresent);
            SystemCondition    internetCondition       = new SystemCondition(SystemConditionType.InternetAvailable);

            /**
             * App store update task registration
             */

            // Trigger the task to run when the app launches.
            _ = RegisterBackgroundTask(
                CheckStoreUpdatesApp, CheckStoreUpdatesEntry,
                appTrigger,
                new[] { internetCondition }
                );
            // Trigger the task to run when the user logs in, i.e. startup, logon.
            _ = RegisterBackgroundTask(
                CheckStoreUpdatesSession, CheckStoreUpdatesEntry,
                sessionConnectedTrigger,
                new[] { userPresentCondition, internetCondition }
                );

            // Run background tasks that should start when the app is launched.
            if (runAppTrigger)
            {
                ApplicationTriggerResult result = await appTrigger.RequestAsync();

                Debug.WriteLine("ApplicationTrigger result is" + result);
            }

            return(true);
        }
Exemple #5
0
        public static async Task <bool> RegisterApplicationTriggerBackgroundTask(string name, string entryPoint)
        {
            bool result = false;

            try
            {
                UnregisterBackgroundTasks();

                BackgroundAccessStatus accessStatus = await BackgroundExecutionManager.RequestAccessAsync();

                if (accessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
                    accessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
                {
                    ApplicationTrigger trigger = new ApplicationTrigger();
                    if (trigger != null)
                    {
                        BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                        builder.Name           = name;
                        builder.TaskEntryPoint = entryPoint;
                        builder.SetTrigger(trigger);
                        builder.Register();

                        ApplicationTriggerResult triggerResult = await trigger.RequestAsync();

                        if (triggerResult == ApplicationTriggerResult.Allowed)
                        {
                            result = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            return(result);
        }
        private async void btnStartLocal_Click(object sender, RoutedEventArgs e)
        {
            LocalClient local = ServiceProvider.Clients.FindLocal();

            if (local == null || task != null)
            {
                return;
            }

            ApplicationTrigger    trigger = new ApplicationTrigger();
            BackgroundTaskBuilder builder = new BackgroundTaskBuilder();

            builder.Name           = TaskName;
            builder.TaskEntryPoint = TaskEntryPoint;
            builder.SetTrigger(trigger);
            task = builder.Register();

            ValueSet args = new ValueSet();

            args["Port"]     = local.Port.ToString();
            args["Delay"]    = local.Delay.ToString();
            args["Interval"] = local.Interval.ToString();

            if (!String.IsNullOrEmpty(local.AuthenticationToken))
            {
                args["AuthenticationToken"] = local.AuthenticationToken;
            }

            ApplicationTriggerResult result = await trigger.RequestAsync(args);

            if (result != ApplicationTriggerResult.Allowed)
            {
                task = null;
            }

            UpdateLocalButtons();
        }