本文整理汇总了C#中Windows.ApplicationModel.Background.BackgroundTaskBuilder.SetTrigger方法的典型用法代码示例。如果您正苦于以下问题:C# BackgroundTaskBuilder.SetTrigger方法的具体用法?C# BackgroundTaskBuilder.SetTrigger怎么用?C# BackgroundTaskBuilder.SetTrigger使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.ApplicationModel.Background.BackgroundTaskBuilder
的用法示例。
在下文中一共展示了BackgroundTaskBuilder.SetTrigger方法的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: RegisterBtn_Click
// Example of a Background Task, not tested
private async void RegisterBtn_Click(object sender, RoutedEventArgs e)
{
string myTaskName = "First Task";
// check if task is already registered
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == myTaskName)
{
Log("Task already registered", "Caution");
return;
}
}
// Windows Phone app must call this to use trigger types (see MSDN)
await BackgroundExecutionManager.RequestAccessAsync();
// register a new task
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "First Task", TaskEntryPoint = "WebServerTask.BackgroundTask" };
taskBuilder.SetTrigger(new TimeTrigger(15, true));
//taskBuilder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
BackgroundTaskRegistration myFirstTask = taskBuilder.Register();
await (new MessageDialog("Task registered")).ShowAsync();
}
示例3: RegisterCortanaIntegrationTask
/// <summary>
/// Registers the background task that makes cortana listen to defined keywords
/// </summary>
private void RegisterCortanaIntegrationTask()
{
// Tracks whether the background task has been registerd or not.
bool taskRegistered = false;
string taskName = "CortanaMake";
// Search background tasks registrations for the task name
foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == taskName)
{
taskRegistered = true;
break;
}
}
// Register the task if it hasn't already been registered
if(!taskRegistered)
{
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = "BackgroundComponent"; // This is the name of a background task that needs to get created. When the task is run,
// the public void Run(IBackgroundTaskInstance taskInstance) will be called in this library.
// We also have to register this in our app manifest.
builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
BackgroundTaskRegistration task = builder.Register();
task.Completed += new BackgroundTaskCompletedEventHandler(OnCortanaMakeTaskCompleted);
}
}
示例4: App
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
var taskRegistered = false;
foreach (var task in Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == "BaconographyW8BackgroundTaskLiveTileUpdater")
{
taskRegistered = true;
break;
}
}
if (!taskRegistered)
{
var builder = new BackgroundTaskBuilder();
builder.Name = "BaconographyW8BackgroundTaskLiveTileUpdater";
builder.TaskEntryPoint = "BaconographyW8BackgroundTask.LiveTileUpdater";
builder.SetTrigger(new TimeTrigger(15, false));
builder.Register();
}
}
示例5: RegisterPushNotifications
/// <summary>
/// The register push notifications.
/// </summary>
private static async void RegisterPushNotifications()
{
const string RegisterPushNotificationName = "RegisterNotifications";
const int RegistrationInterval = 10 * 24 * 60;
if (GetRegisteredTask(RegisterPushNotificationName) != null)
{
return;
}
await BackgroundExecutionManager.RequestAccessAsync();
var taskBuilder = new BackgroundTaskBuilder
{
Name = RegisterPushNotificationName,
TaskEntryPoint = typeof(RegisterPushNotificationTask).FullName
};
var trigger = new TimeTrigger(RegistrationInterval, false);
taskBuilder.SetTrigger(trigger);
var internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
taskBuilder.AddCondition(internetCondition);
try
{
taskBuilder.Register();
}
catch (Exception exception)
{
}
}
示例6: 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;
}
示例7: StartLocationTracker
public void StartLocationTracker()
{
if( CanRunAsTask() )
{
BackgroundTaskBuilder geolocTaskBuilder = new BackgroundTaskBuilder();
geolocTaskBuilder.Name = SampleBackgroundTaskName;
geolocTaskBuilder.TaskEntryPoint = SampleBackgroundTaskEntryPoint;
// Create a new timer triggering at a 15 minute interval
var trigger = new TimeTrigger( UpdateInterval, true );
// Associate the timer trigger with the background task builder
geolocTaskBuilder.SetTrigger( trigger );
// Register the background task
_geolocTask = geolocTaskBuilder.Register();
// Associate an event handler with the new background task
_geolocTask.Completed += new BackgroundTaskCompletedEventHandler( OnCompleted );
}
else
{
task = new LocationBackgroundTask();
timer = ThreadPoolTimer.CreatePeriodicTimer( TimerElapsed, new TimeSpan( TimeSpan.TicksPerMillisecond * 30000 ) );
}
}
示例8: 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);
}
}
示例9: 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;
}
示例10: Register
/// <summary>
/// 屏幕点亮时执行该任务:UserPresent
/// http://msdn.microsoft.com/library/windows/apps/windows.applicationmodel.background.systemtriggertype.aspx
/// </summary>
/// <param name="taskEntryPoint">后台任务名</param>
public static async void Register(string taskEntryPoint)
{
BackgroundExecutionManager.RemoveAccess();
var status = await BackgroundExecutionManager.RequestAccessAsync();
if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.Denied)
{
await new MessageDialog("系统关闭了后台运行,请前往‘系统设置’进行设置").ShowAsync();
return;
}
else
{
var taskReg = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(t => t.Name.Equals(LIVETILETASK)) as BackgroundTaskRegistration;
if (taskReg == null)
{
//注册任务
var taskBuilder = new BackgroundTaskBuilder
{
Name = LIVETILETASK,
TaskEntryPoint = taskEntryPoint
};
var trigger = new SystemTrigger(SystemTriggerType.UserPresent, false);
taskBuilder.SetTrigger(trigger);
taskBuilder.Register();
}
else
{
return;
}
}
}
示例11: 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);
}
}
示例12: RegisterBackGroundTask
public static BackgroundTaskRegistration RegisterBackGroundTask(string taskEntryPoint,
string taskName,
IBackgroundTrigger trigger,
IBackgroundCondition condition)
{
//Check if its alterady Registrated
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == taskName)
{
return (BackgroundTaskRegistration)(cur.Value);
}
}
//Register the Task
var builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = taskEntryPoint;
if (trigger != null)
{
builder.SetTrigger(trigger);
}
if (condition != null)
{
builder.AddCondition(condition);
}
BackgroundTaskRegistration task = builder.Register();
return task;
}
示例13: RegisterTask
/// <summary>
/// Method reponsible for registering the background task
/// </summary>
private async void RegisterTask()
{
var taskRegistered = Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks.Select(task => task.Value.Name == nameof(TileUpdaterTask)).Count() > 0;
if (taskRegistered)
{
this.StatusLabel.Text = "Task already registered!";
}
else
{
// Check if task is registred
BackgroundAccessStatus allowed = await BackgroundExecutionManager.RequestAccessAsync();
if (allowed == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || allowed == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
{
var task = new BackgroundTaskBuilder
{
Name = nameof(TileUpdaterTask),
CancelOnConditionLoss = false,
TaskEntryPoint = typeof(TileUpdaterTask).ToString(),
};
task.SetTrigger(new TimeTrigger(15, false));
task.AddCondition(new SystemCondition(SystemConditionType.UserPresent));
task.Register();
this.StatusLabel.Text = "Task has been registered!";
}
else
{
this.StatusLabel.Text = "Not allowd to register task!";
}
}
}
示例14: RegisterBackgroundTask
/// <summary>
/// We proceed to register the task if it hasn't been registered yet. Otherwise, we fetch the registration
/// and retrieve the ApplicationTrigger from there.
/// </summary>
private async void RegisterBackgroundTask()
{
try
{
if (!BackgroundTaskRegistration.AllTasks.Any(reg => reg.Value.Name == TileRegistrationName))
{
// Configure task parameters
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.TaskEntryPoint = typeof(TileUpdateTask).FullName;
builder.Name = TileRegistrationName;
// Remember to set an ApplicationTrigger so we can run it on demand later
this.backgroundTrigger = new ApplicationTrigger();
builder.SetTrigger(backgroundTrigger);
builder.Register();
MessageDialog infoDialog = new MessageDialog("Background task successfully registered.", "Info");
await infoDialog.ShowAsync();
}
else
{
// Fetch registration details and trigger if already existing
var registration = BackgroundTaskRegistration.AllTasks.FirstOrDefault(reg => reg.Value.Name == TileRegistrationName).Value as BackgroundTaskRegistration;
this.backgroundTrigger = registration.Trigger as ApplicationTrigger;
MessageDialog infoDialog = new MessageDialog("Background task registration data successfully retrieved.", "Info");
await infoDialog.ShowAsync();
}
}
catch (Exception ex)
{
MessageDialog errorDialog = new MessageDialog("There was an error while trying to register the background task.", "Error");
errorDialog.ShowAsync();
}
}
示例15: 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...
}
}