当前位置: 首页>>代码示例>>C#>>正文


C# BackgroundTaskBuilder.AddCondition方法代码示例

本文整理汇总了C#中Windows.ApplicationModel.Background.BackgroundTaskBuilder.AddCondition方法的典型用法代码示例。如果您正苦于以下问题:C# BackgroundTaskBuilder.AddCondition方法的具体用法?C# BackgroundTaskBuilder.AddCondition怎么用?C# BackgroundTaskBuilder.AddCondition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Windows.ApplicationModel.Background.BackgroundTaskBuilder的用法示例。


在下文中一共展示了BackgroundTaskBuilder.AddCondition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: addBackgroundTask

        /// <summary>
        /// 增加后台任务
        /// </summary>
        private async void addBackgroundTask()
        {
            bool taskRegistered = false;
            //判断是否已经注册过了
            taskRegistered = BackgroundTaskRegistration.AllTasks.Any(x => x.Value.Name == exampleTaskName);

            if (!taskRegistered)
            {
                BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
                var builder = new BackgroundTaskBuilder();
                builder.Name = exampleTaskName;
                builder.TaskEntryPoint = "Tasks.LetterBackgroundTask";
                //后台触发器,可多个
                builder.SetTrigger(new SystemTrigger(SystemTriggerType.NetworkStateChange, false));
                builder.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
                //builder.SetTrigger(new MaintenanceTrigger(15, false)); //定时后台任务
                builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));

                BackgroundTaskRegistration task = builder.Register();
            }
            else
            {
                var cur = BackgroundTaskRegistration.AllTasks.FirstOrDefault(x => x.Value.Name == exampleTaskName);
                BackgroundTaskRegistration task = (BackgroundTaskRegistration)(cur.Value);
            }

        }
开发者ID:cappuccinoqi,项目名称:Date_Windows,代码行数:30,代码来源:App.xaml.cs

示例2: RegisterBackgroundTask

        /*
         * 在 Windows Phone 上,你必须在尝试注册任何后台任务之前调用 RequestAccessAsync
         */

        /// <summary>
        /// 注册后台任务
        /// </summary>
        /// <param name="taskEntryPoint">任务的入口点</param>
        /// <param name="name">任务名称</param>
        /// <param name="trigger">轮询时间</param>
        /// <param name="condition">系统事件</param>
        /// <returns></returns>
        public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
        {
            BackgroundTaskRegistration task = null;
            if (TaskRequiresBackgroundAccess(name))
            {
                await BackgroundExecutionManager.RequestAccessAsync();
            }
            var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
            if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
                backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
            {

                var builder = new BackgroundTaskBuilder();

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

                if (condition != null)
                {
                    builder.AddCondition(condition);
                    builder.CancelOnConditionLoss = true;
                }
                task = builder.Register();
            }
            return task;
        }
开发者ID:jevonsflash,项目名称:Weather,代码行数:39,代码来源:BackgroundTaskHelper.cs

示例3: RegisterBackgroundTasks

        private async void RegisterBackgroundTasks()
        {
            var builder = new BackgroundTaskBuilder
            {
                Name = "BackgroundUpdateReadingList",
                TaskEntryPoint = "EasyReader.BackgroundTasks.UpdateReadingList"
            };

            try
            {
                await BackgroundExecutionManager.RequestAccessAsync();
            }
            catch (Exception)
            {
                
            }

            IBackgroundTrigger trigger = new TimeTrigger(15, true);
            builder.SetTrigger(trigger);

            IBackgroundCondition condition = new SystemCondition(SystemConditionType.InternetAvailable);
            builder.AddCondition(condition);

            IBackgroundTaskRegistration task = builder.Register();

            task.Progress += task_Progress;
            task.Completed += task_Completed;
        }
开发者ID:beaugunderson,项目名称:EasyReader,代码行数:28,代码来源:App.xaml.cs

示例4: RegisterTaskButton_Click

        private void RegisterTaskButton_Click(Object sender, RoutedEventArgs e)
        {
            if (GetRegisteredTask() == null)
            {
                BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
                MaintenanceTrigger trigger = new MaintenanceTrigger(MAINTENANCE_INTERVAL, false);
                taskBuilder.SetTrigger(trigger);
                taskBuilder.TaskEntryPoint = PUSH_NOTIFICATIONS_TASK_ENTRY_POINT;
                taskBuilder.Name = PUSH_NOTIFICATIONS_TASK_NAME;

                SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
                taskBuilder.AddCondition(internetCondition);

                try
                {
                    taskBuilder.Register();
                    rootPage.NotifyUser("Task registered", NotifyType.StatusMessage);
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("Error registering task: " + ex.Message, NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUser("Task already registered", NotifyType.ErrorMessage);
            }
        }
开发者ID:WHISHWORKS,项目名称:MuleSummit2013,代码行数:28,代码来源:ScenarioInput2.xaml.cs

示例5: RegisterTasks

        private async Task RegisterTasks()
        {
            // Applications must have lock screen privileges in order to receive raw notifications
            var backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();

            // Make sure the user allowed privileges
            if (backgroundStatus != BackgroundAccessStatus.Denied && backgroundStatus != BackgroundAccessStatus.Unspecified)
            {
                var systemEventTaskBuilder = new BackgroundTaskBuilder();
                var systemEventTrigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
                systemEventTaskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
                systemEventTaskBuilder.SetTrigger(systemEventTrigger);

                // Background tasks must live in separate DLL, and be included in the package manifest
                // Also, make sure that your main application project includes a reference to this DLL
                systemEventTaskBuilder.TaskEntryPoint = SystemEventTaskEntryPoint;
                systemEventTaskBuilder.Name = SystemEventTaskName;

                try
                {
                    var task = systemEventTaskBuilder.Register();
                    task.Completed += this.SystemEventBackgroundTaskCompleted;
                }
                catch (Exception ex)
                {
                    // Should probably log this somewhere...
                }
            }
            else
            {
                // Should probably log this somewhere...
            }
        }
开发者ID:gep13,项目名称:WindowsPhoneSample,代码行数:33,代码来源:BackgroundTaskService.cs

示例6: PushNotifications

        /// <summary>
        /// The register task.
        /// </summary>
        private static async void PushNotifications()
        {
            const string PushNotificationTaskName = "ToastNotifications";

            if (GetRegisteredTask(PushNotificationTaskName) != null)
            {
                return;
            }

            var ns = new NotificationStore();
            ns.Register();

            await BackgroundExecutionManager.RequestAccessAsync();

            await ObtainLockScreenAccess();
            var taskBuilder = new BackgroundTaskBuilder
                                  {
                                      Name = PushNotificationTaskName,
                                      TaskEntryPoint = typeof(PushNotificationTask).FullName
                                  };

            var trigger = new PushNotificationTrigger();
            taskBuilder.SetTrigger(trigger);

            var internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
            taskBuilder.AddCondition(internetCondition);

            try
            {
                taskBuilder.Register();
            }
            catch (Exception exception)
            {
            }
        }
开发者ID:jlkalberer,项目名称:Linkslap.WP,代码行数:38,代码来源:RegisterTasks.cs

示例7: RegisterBackgroundTasks

        private async void RegisterBackgroundTasks()
        {
            var access = await BackgroundExecutionManager.RequestAccessAsync();
            if (access == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || access == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
            {
                TimeTrigger taskTrigger = new TimeTrigger(15, false);
                foreach (var _task in BackgroundTaskRegistration.AllTasks)
                {
                    if (_task.Value.Name == "WPGetListUpdateTimer" || _task.Value.Name=="WPGetListUpdateInternet")
                    {
                        _task.Value.Unregister(true);
                    }
                }
                SystemTrigger striger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
                var bgTaskBuilder = new BackgroundTaskBuilder();
                bgTaskBuilder.Name = "WPGetListUpdateTimer";
                bgTaskBuilder.TaskEntryPoint = "BackgroundTaskWinMD.WPGetListUpdateBackgroundTask";
                bgTaskBuilder.SetTrigger(taskTrigger);
                // условие, согласно которому триггер будет выполнен только если интернет доступен
                SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
                bgTaskBuilder.AddCondition(internetCondition);
                BackgroundTaskRegistration task = bgTaskBuilder.Register();

                bgTaskBuilder = new BackgroundTaskBuilder();
                bgTaskBuilder.Name = "WPGetListUpdateInternet";
                bgTaskBuilder.TaskEntryPoint = "BackgroundTaskWinMD.WPGetListUpdateBackgroundTask";
                bgTaskBuilder.SetTrigger(striger);
                
                bgTaskBuilder.AddCondition(internetCondition);
                BackgroundTaskRegistration task2 = bgTaskBuilder.Register();

            }
        }
开发者ID:polushinmk,项目名称:WordPressPluginsUpdate,代码行数:33,代码来源:App.xaml.cs

示例8: RegisterBackgroundTask

        public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
        {
            //always unregister for now. 
            foreach (var bgt in BackgroundTaskRegistration.AllTasks)
            {
                bgt.Value.Unregister(true);
            }
            //in UWP, you have to ask for access. Just like in WP
            var access = await BackgroundExecutionManager.RequestAccessAsync();
            if (access == BackgroundAccessStatus.Denied)
            {
                throw new UnauthorizedAccessException();
            }

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

            if (condition != null)
            {
                builder.AddCondition(condition);
                builder.CancelOnConditionLoss = true;
            }
            BackgroundTaskRegistration task = builder.Register();

            return task;
        }
开发者ID:bospoort,项目名称:Win10UWPDemo,代码行数:28,代码来源:MainPage.xaml.cs

示例9: RegisterBackgroundTask

        private async Task RegisterBackgroundTask()
        {
            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                if (task.Value.Name == UPDATE_TILE_TASK_NAME)
                {
                    // 已经注册后台任务。
                    return;
                }
            }

            BackgroundAccessStatus access = await BackgroundExecutionManager.RequestAccessAsync();

            if (access != BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
            {
                // 没有权限。
                return;
            }

            BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
            // 仅在网络可用下执行后台任务。
            builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));

            builder.Name = UPDATE_TILE_TASK_NAME;
            builder.TaskEntryPoint = "BingoWallpaper.BackgroundTask.UpdateTileTask";
            // 每 90 分钟触发一次。
            builder.SetTrigger(new TimeTrigger(90, false));

            BackgroundTaskRegistration registration = builder.Register();
        }
开发者ID:h82258652,项目名称:MSDevUnion.BingWallpaper,代码行数:30,代码来源:ExtendedSplashScreen.xaml.cs

示例10: RegisterTaskAsync

        public static IAsyncOperation<BackgroundTaskRegistration> RegisterTaskAsync()
        {
            return AsyncInfo.Run(async (cancellationToken) =>
            {
                if (IsTaskRegistered())
                    return _current;

                await BackgroundExecutionManager.RequestAccessAsync();


                //http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.timetrigger.aspx
                IBackgroundTrigger trigger = new TimeTrigger(6*60, false); //6 hours

                // Builds the background task.
                BackgroundTaskBuilder builder = new BackgroundTaskBuilder();

                builder.Name = FriendlyName;
                builder.TaskEntryPoint = typeof(BackgroundDemoTask).FullName;
                builder.SetTrigger(trigger);

                SystemCondition condition = new SystemCondition(SystemConditionType.InternetAvailable);
                builder.AddCondition(condition);

                // Registers the background task, and get back a BackgroundTaskRegistration object
                // representing the registered task.
                _current = builder.Register();
                return _current;
            });
        }
开发者ID:jmservera,项目名称:UniversalBackgroundApp,代码行数:29,代码来源:BackgroundDemoTask.cs

示例11: RegisterBackgroundTask

        /// <summary>
        /// Register a background task with the specified taskEntryPoint, name, trigger,
        /// and condition (optional).
        /// </summary>
        /// <param name="taskEntryPoint">Task entry point for the background task.</param>
        /// <param name="name">A name for the background task.</param>
        /// <param name="trigger">The trigger for the background task.</param>
        /// <param name="condition">An optional conditional event that must be true for the task to fire.</param>
        public static BackgroundTaskRegistration RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
        {
            var builder = new BackgroundTaskBuilder();

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

            if (condition != null)
            {
                builder.AddCondition(condition);
            }

            BackgroundTaskRegistration task = builder.Register();

            UpdateBackgroundTaskStatus(name, true);

            //
            // Remove previous completion status from local settings.
            //
            var settings = ApplicationData.Current.LocalSettings;
            settings.Values.Remove(name);

            return task;
        }
开发者ID:AlyCrunch,项目名称:samples,代码行数:33,代码来源:Constants.cs

示例12: BackRegister

 public static async void BackRegister()
 {
     string taskName = "back_notifi"; //后台任务名称
     string entryPoint = "BackPush.NotifiBackTask"; //入口点
     // 检查是否许后台任务
     var result = await BackgroundExecutionManager.RequestAccessAsync();
     if (result == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
     {
         // 检查是否已经注册后台任务
         var task = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault((t) => t.Name == taskName);
         // 如果未注册,则进行注册
         if (task == null)
         {
             BackgroundTaskBuilder tb = new BackgroundTaskBuilder();
             tb.TaskEntryPoint = entryPoint;
             tb.Name = taskName;
             // 触发器为推送通知触发器
             tb.SetTrigger(new PushNotificationTrigger());
             // 运行条件为网络可用
             tb.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
             // 注册
             tb.Register();
         }
     }
 }
开发者ID:x01673,项目名称:dreaming,代码行数:25,代码来源:BackgroundTaskHelp.cs

示例13: registerButton_Click

        private async void registerButton_Click(object sender, RoutedEventArgs e)
        {
            TimeTrigger timetrigger = new TimeTrigger(30, false);
            PushNotificationTrigger notificationTrigger = new PushNotificationTrigger();
            


            var SampleTask = new BackgroundTaskBuilder(); //创建后台任务实例
            SampleTask.Name = "Reminder";  //指定后台任务名称
            SampleTask.TaskEntryPoint = "ReminderComponent.Reminder";//指定后台任务名称
            SampleTask.SetTrigger(timetrigger);//指定后台任务的触发器

            SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
            SampleTask.AddCondition(internetCondition);

            var access = await BackgroundExecutionManager.RequestAccessAsync();
            if (access == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
            {
                BackgroundTaskRegistration task = SampleTask.Register();
                task.Progress += Value_Progress; ;
                task.Completed += Value_Completed; ;
                UpdateUI("", "注册成功");

                registerButton.IsEnabled = false;
                cancelButtton.IsEnabled = true;

                var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
                settings.Values.Remove(task.Name);
            }
            else if (access == BackgroundAccessStatus.Denied)//用户禁用后台任务或后台任务数量已达最大
            {
                await new MessageDialog("您已禁用后台任务或后台任务数量已达最大!").ShowAsync();
            }

        }
开发者ID:fqncom,项目名称:tomcraporigami,代码行数:35,代码来源:BackTaskDemo.xaml.cs

示例14: UpdateTile

        private async Task UpdateTile()
        {
            var result = await BackgroundExecutionManager.RequestAccessAsync();
            if (result == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
                result == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
            {
                foreach (var task in BackgroundTaskRegistration.AllTasks)
                {
                    if (task.Value.Name == TASK_NAME)
                        task.Value.Unregister(true);

                    //task.Value.Progress += new BackgroundTaskProgressEventHandler(OnProgress);
                    //task.Value.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
                }

                BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                var condition = new SystemCondition(SystemConditionType.InternetAvailable);

                builder.Name = TASK_NAME;
                builder.AddCondition(condition);
                builder.TaskEntryPoint = TASK_ENTRY;
                builder.SetTrigger(new TimeTrigger(15, false));
                var registration = builder.Register();

                registration.Progress += new BackgroundTaskProgressEventHandler(task_Progress);
                registration.Completed += new BackgroundTaskCompletedEventHandler(task_Completed);

            }
        }
开发者ID:stavrianosy,项目名称:BudgetManagementAssistant,代码行数:29,代码来源:MainPage.xaml.cs

示例15: RegisterBackgroundTask

        private async static Task<BackgroundTaskRegistration> RegisterBackgroundTask(Type taskEntryPoint, string taskName, IBackgroundTrigger trigger, IBackgroundCondition condition)
        {
            var status = await BackgroundExecutionManager.RequestAccessAsync();
            if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.Denied)
            {
                return null;
            }

            foreach (var cur in BackgroundTaskRegistration.AllTasks)
            {
                if (cur.Value.Name == taskName)
                {
                    cur.Value.Unregister(true);
                }
            }

            var builder = new BackgroundTaskBuilder
            {
                Name = taskName,
                TaskEntryPoint = taskEntryPoint.FullName
            };

            builder.SetTrigger(trigger);

            if (condition != null)
            {
                builder.AddCondition(condition);
            }

            return builder.Register(); ;
        }
开发者ID:feong,项目名称:CC,代码行数:31,代码来源:BackgroundTaskRegister.cs


注:本文中的Windows.ApplicationModel.Background.BackgroundTaskBuilder.AddCondition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。