本文整理汇总了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);
}
}
示例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;
}
示例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;
}
示例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);
}
}
示例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...
}
}
示例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)
{
}
}
示例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();
}
}
示例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;
}
示例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();
}
示例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;
});
}
示例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;
}
示例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();
}
}
}
示例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();
}
}
示例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);
}
}
示例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(); ;
}