/// <summary> /// This is the background task completion handler. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void OnBackgroundTaskCompleted(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs e) { // Dispatch to the UI thread to display the output. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // An exception may be thrown if an error occurs in the background task. try { e.CheckResult(); if (ApplicationData.Current.LocalSettings.Values.ContainsKey("TaskCancelationReason")) { string cancelationReason = (string)ApplicationData.Current.LocalSettings.Values["TaskCancelationReason"]; Debug.WriteLine("Background task was stopped, reason: " + cancelationReason); } } catch (Exception ex) { Debug.WriteLine("Exception in background task: " + ex.Message); } }); // Unregister the background task and let the remaining task finish until completion. if (null != _deviceUseBackgroundTaskRegistration) { _deviceUseBackgroundTaskRegistration.Unregister(false); _deviceUseBackgroundTaskRegistration = null; } }
public async Task <bool> Toggle() { if (started) { registration.Unregister(true); registration = null; Debug.WriteLine("TOGGLE failllllllllllllllll"); return(false); } else { try { await BackgroundExecutionManager.RequestAccessAsync(); BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = typeof(AmazonPriceTrackerBackground.BackgroundTask).FullName; TimeTrigger trigger = new TimeTrigger(15, false); Debug.WriteLine("Process Name: " + builder.Name); builder.SetTrigger(trigger); builder.TaskEntryPoint = "AmazonPriceTrackerBackground.BackgroundTask"; builder.Register(); registration = BackgroundTaskRegistration.AllTasks.Values.First(); Debug.WriteLine("TOGGLE SUCCESS"); return(true); } catch (Exception e) { Debug.WriteLine(e.StackTrace); Debug.WriteLine(e.Message); Debug.WriteLine("TOGGLE Fail"); return(false); } } }
public static void Register() { foreach (var iter in BackgroundTaskRegistration.AllTasks) { IBackgroundTaskRegistration mytask = iter.Value; if (mytask.Name == "ExampleBackgroundTask") { mytask.Unregister(true); break; } } var builder = new BackgroundTaskBuilder(); PushNotificationTrigger trigger = new PushNotificationTrigger(); builder.SetTrigger(trigger); builder.Name = "ExampleBackgroundTask"; builder.TaskEntryPoint = "Usergrid.Notifications.ExampleBackgroundTask"; ExampleBackgroundTask.task = builder.Register(); task.Progress += task_Progress; task.Completed += task_Completed; }
private void UnregisterBackgroundTask(IBackgroundTaskRegistration taskReg) { taskReg.Unregister(true); ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; settings.Values["eventCount"] = (uint)0; }
private void UnregisterrListenerTask() { if (taskRegistration != null) { Debug.WriteLine("UnregisterListenerTask: unregister " + taskRegistration.Name + " / " + taskRegistration.TaskId); MainPage.Current.UpdateDebugMessage(this, "UnregisterListenerTask: unregister inapp " + taskRegistration.Name + " / " + taskRegistration.TaskId); taskRegistration.Completed -= OnMessageListenerTaskCompleted; taskRegistration.Progress -= OnMessageListenerTaskProgress; taskRegistration.Unregister(true); taskRegistration = null; //rootPage.NotifyUser("Background publisher unregistered.", NotifyType.StatusMessage); } else { // At this point we assume we haven't found any existing tasks matching the one we want to unregister //rootPage.NotifyUser("No registered background publisher found.", NotifyType.StatusMessage); foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == taskName) { Debug.WriteLine("UnregisterListenerTask: unregister " + task.Value.Name + " / " + task.Value.TaskId); MainPage.Current.UpdateDebugMessage(this, "UnregisterListenerTask: unregister " + task.Value.Name + " / " + task.Value.TaskId); task.Value.Completed -= OnMessageListenerTaskCompleted; task.Value.Progress -= OnMessageListenerTaskProgress; task.Value.Unregister(true); break; } } } ApplicationData.Current.LocalSettings.Values[taskName + ".Cancel"] = null; }
public static async void RegisterAndRunAsync(DeviceInformation devInfo) { var taskName = typeof(CheckHeartRateInBackgroundTask).ToString(); IBackgroundTaskRegistration checkHeartRateInBackground = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(t => t.Name == taskName); if (band.IsConnected() && band.Identity.IsAuthenticated()) { if (checkHeartRateInBackground != null) { // if the task is already executing, unregister it checkHeartRateInBackground.Unregister(true); } var devTrigger = new DeviceUseTrigger(); var taskBuilder = new BackgroundTaskBuilder { Name = taskName, TaskEntryPoint = typeof(CheckHeartRateInBackgroundTask).ToString(), IsNetworkRequested = false }; taskBuilder.SetTrigger(devTrigger); BackgroundTaskRegistration task = taskBuilder.Register(); task.Completed += Task_Completed; task.Progress += Task_Progress; await devTrigger.RequestAsync(devInfo.Id); } }
public void UnregisterBackgroundTask() { if (IsRegistered) { _backgroundTask.Unregister(false); } }
public static async void RegisterAndRunAsync() { var taskName = typeof(CheckHeartRateInBackgroundTask).Name; IBackgroundTaskRegistration checkHeartRateInBackground = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(t => t.Name == taskName); if (band.IsConnected() && band.Identity.IsAuthenticated()) { if (checkHeartRateInBackground != null) { checkHeartRateInBackground.Unregister(true); } var deviceTrigger = new DeviceUseTrigger(); var deviceInfo = await band.Identity.GetPairedBand(); var taskBuilder = new BackgroundTaskBuilder { Name = taskName, TaskEntryPoint = typeof(CheckHeartRateInBackgroundTask).ToString(), IsNetworkRequested = false }; taskBuilder.SetTrigger(deviceTrigger); BackgroundTaskRegistration task = taskBuilder.Register(); await deviceTrigger.RequestAsync(deviceInfo.Id); } }
public async Task <bool> Toggle() { if (started) { registration.Unregister(true); registration = null; return(false); } else { try { await BackgroundExecutionManager.RequestAccessAsync(); BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = typeof(Agent.Background.Trigger).FullName; builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false)); builder.TaskEntryPoint = builder.Name; builder.Register(); registration = BackgroundTaskRegistration.AllTasks.Values.First(); return(true); } catch { return(false); } } }
/// <summary> /// Invoked as an event handler when the Stop button is pressed. /// </summary> /// <param name="sender">Instance that triggered the event.</param> /// <param name="e">Event data describing the conditions that led to the event.</param> private void StopButton_Click(object sender, RoutedEventArgs e) { // Unregistering the background task will stop advertising if this is the only client requesting // First get the existing tasks to see if we already registered for it if (taskRegistration != null) { taskRegistration.Unregister(true); taskRegistration = null; rootPage.NotifyUser("Background publisher unregistered.", NotifyType.StatusMessage); } else { // At this point we assume we haven't found any existing tasks matching the one we want to unregister rootPage.NotifyUser("No registered background publisher found.", NotifyType.StatusMessage); } }
void Stop() { if (backgroundTaskRegistration != null) { backgroundTaskRegistration.Unregister(true); } UpdateTask(); }
private void UnregisterGeoFence() { if (null != _geofenceTask) { _geofenceTask.Unregister(true); _geofenceTask = null; } }
public void StopLocationTracker() { if (null != _geolocTask) { _geolocTask.Unregister(true); _geolocTask = null; } }
public void EnsureState(bool shouldStart) { if (taskRegistration == null) { foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == taskName) { taskRegistration = task.Value; Debug.WriteLine("Task found: " + task.Value.Name + " / " + task.Value.TaskId); MainPage.Current.UpdateDebugMessage(this, "Task found: " + task.Value.Name + " / " + task.Value.TaskId); break; } } } if (shouldStart) { if (taskRegistration == null) { var ignored = EnableBT(true); RegisterBTWatcherTask(); } else { if (App.previousExecState == Windows.ApplicationModel.Activation.ApplicationExecutionState.NotRunning) { taskRegistration.Unregister(true); taskRegistration = null; var ignored = EnableBT(true); RegisterBTWatcherTask(); } else { taskRegistration.Completed += OnBTWatcherTaskCompleted; } } } else { if (taskRegistration != null) { UnregisterBTWatcherTask(); } } }
void RemoveBackgroundTask() { if (backgroundTask != null) { backgroundTask.Unregister(true); BackgroundExecutionManager.RemoveAccess(); } UpdateUI(); }
private void unregisterBackgroundTask() { // Unregister the background task if (_geofenceTask != null) { _geofenceTask.Unregister(true); _geofenceTask = null; } System.Diagnostics.Debug.WriteLine("Geofence background task unregistered"); }
// Handle request to unregister the background task private void UnregisterTask() { IBackgroundTaskRegistration taskRegistration = MainPage.FindBackgroundTaskRegistration(); if (taskRegistration != null) { taskRegistration.Unregister(true); rootPage.NotifyUser("Unregistered Caller ID background task.", NotifyType.StatusMessage); } UpdateBackgroundTaskUIState(false); }
private void btnStopLocal_Click(object sender, RoutedEventArgs e) { if (task == null) { return; } task.Unregister(true); task = null; UpdateLocalButtons(); }
/// <summary> /// This is the click handler for the 'Unregister' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UnregisterBackgroundTask(object sender, RoutedEventArgs e) { // Unregister the background task if (null != _geofenceTask) { _geofenceTask.Unregister(true); _geofenceTask = null; } _rootPage.NotifyUser("Geofence background task unregistered", NotifyType.StatusMessage); UpdateButtonStates(/*registered:*/ false); }
private void UnregisterBackgroundTask() { // Loop through all background tasks and unregister any that matches the givn name. foreach (var keyValuePair in BackgroundTaskRegistration.AllTasks) { IBackgroundTaskRegistration task = keyValuePair.Value; if (task.Name == taskName) { task.Completed -= taskCompletedHandler; task.Unregister(true); } } }
/// <summary> /// This is the click handler for the 'Unregister' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UnregisterBackgroundTask(object sender, RoutedEventArgs e) { // Unregister the background task if (null != _visitTask) { _visitTask.Unregister(true); _visitTask.Completed -= OnCompleted; _visitTask = null; } _rootPage.NotifyUser("Visit background task unregistered", NotifyType.StatusMessage); UpdateButtonStates(); }
private bool UnregisterBackgroundTask() { foreach (var iter in BackgroundTaskRegistration.AllTasks) { IBackgroundTaskRegistration task = iter.Value; if (task.Name == PushTaskName) { task.Unregister(true); return(true); } } return(false); }
/// <summary> /// Unregisters the background task from Windows. /// </summary> public static void UnregisterBackgroundTask(Type backgroundTaskType, bool cancel) { var query = from task in BackgroundTaskRegistration.AllTasks where task.Value.Name == backgroundTaskType.Name select task.Value; IBackgroundTaskRegistration registration = query.FirstOrDefault(); if (registration != null) { registration.Unregister(cancel); } }
async private void activeLocation_Toggled(object sender, RoutedEventArgs e) { if (activeLocation.IsOn == true && _geolocTask == null) { try { BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); BackgroundTaskBuilder geolocTaskBuilder = new BackgroundTaskBuilder(); geolocTaskBuilder.Name = BackgroundTaskName; geolocTaskBuilder.TaskEntryPoint = BackgroundTaskEntryPoint; var trigger = new TimeTrigger(15, false); geolocTaskBuilder.SetTrigger(trigger); _geolocTask = geolocTaskBuilder.Register(); _geolocTask.Completed += OnCompleted; switch (backgroundAccessStatus) { case BackgroundAccessStatus.Unspecified: case BackgroundAccessStatus.Denied: Status.Text = "Impossible de fonctionner en arrière-plan. La demande doit être ajouté à l'écran de verrouillage."; break; default: Status.Text = "Enregister."; RequestLocationAccess(); break; } } catch (Exception ex) { Status.Text = ex.ToString(); } } if (activeLocation.IsOn == false) { if (null != _geolocTask) { _geolocTask.Unregister(true); _geolocTask = null; } MobilePosition_Latitude.Text = "Pas dedata"; MobilePosition_Longitude.Text = "Pas de data"; MobilePosition_Accuracy.Text = "Pas de data"; } }
public static void UnregisterTask() { IBackgroundTaskRegistration task = GetRegisteredTask(); if (task != null) { task.Unregister(true); //rootPage.NotifyUser("Task unregistered", NotifyType.StatusMessage); } else { //rootPage.NotifyUser("Task not registered", NotifyType.ErrorMessage); } }
/// <summary> /// This is the click handler for the 'Unregister' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void Unregister() { // Unregister the background task if (null != _geofenceTask) { _geofenceTask.Unregister(true); _geofenceTask = null; } var successDialog = new MessageDialog("Geofence background task unregistered"); await successDialog.ShowAsync(); this.IsTaskRegistered = false; }
private void UnregisterTaskButton_Click(Object sender, RoutedEventArgs e) { IBackgroundTaskRegistration task = GetRegisteredTask(); if (task != null) { task.Unregister(true); rootPage.NotifyUser("Task unregistered", NotifyType.StatusMessage); } else { rootPage.NotifyUser("Task not registered", NotifyType.ErrorMessage); } }
/// <summary> /// Helper to unregister notification toasts /// </summary> /// <param name="taskRegistration"></param> /// <returns></returns> private async Task TaskUnregisterHelperAsync(IBackgroundTaskRegistration taskRegistration) { // Do the unregistration taskRegistration.Unregister(true); // Don't need to keep track of the toast name data in local settings anymore. ApplicationData.Current.LocalSettings.Values.Remove(_toastName); // If current device isn't active, we don't want to drain the battery by listening // for notifications on this device. if (GlobalSettings.SelectedDevice != ServiceM.DeviceM) { await UnregisterNotificationAsync(); } }
private bool UnregisterBackgroundTask() { bool result = false; foreach (var iter in BackgroundTaskRegistration.AllTasks) { IBackgroundTaskRegistration task = iter.Value; if (task.Name == SAMPLE_TASK_NAME) { task.Unregister(true); result = true; } } return(result); }
/// <summary> /// This is the click handler for the 'Unregister' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UnregisterBackgroundTask(object sender, RoutedEventArgs e) { // Unregister the background task if (null != _geolocTask) { _geolocTask.Unregister(true); _geolocTask = null; } ScenarioOutput_Latitude.Text = "No data"; ScenarioOutput_Longitude.Text = "No data"; ScenarioOutput_Accuracy.Text = "No data"; UpdateButtonStates(/*registered:*/ false); _rootPage.NotifyUser("Background task unregistered", NotifyType.StatusMessage); }
/// <summary> /// This is the event handler for background task completion. /// </summary> /// <param name="task">The task that is reporting completion.</param> /// <param name="args">The completion report arguments.</param> private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args) { string status = "Completed and Unregistered"; try { // check for the result from the background task args.CheckResult(); } catch (Exception e) { status = e.Message; } // Pedometer background triggers are unique in a way as the background // events are one-shot - Once the step-goal associated with the original // task registration has been reached, that step count is a thing-of-past // and shall not fire as the stepcount on that pedometer changes. // Unregister the background task or update the step goals here. task.Unregister(false); backgroundTaskRegistered = false; UpdateUIAsync(status); }