async void RegisterTask(DeviceWatcherTrigger deviceWatcherTrigger) { System.Diagnostics.Debug.WriteLine("[RegisterTask] Register the background task."); // // Check for existing registrations of this background task. // BackgroundExecutionManager.RemoveAccess(); var access = await BackgroundExecutionManager.RequestAccessAsync(); foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == authBGTaskName) { taskRegistered = true; //task.Value.Unregister(true); break; } } if (!taskRegistered) { if (access == BackgroundAccessStatus.AllowedSubjectToSystemPolicy) { BackgroundTaskBuilder authTaskBuilder = new BackgroundTaskBuilder(); authTaskBuilder.Name = authBGTaskName; SecondaryAuthenticationFactorAuthenticationTrigger myTrigger = new SecondaryAuthenticationFactorAuthenticationTrigger(); authTaskBuilder.TaskEntryPoint = authBGTaskEntryPoint; authTaskBuilder.SetTrigger(myTrigger); BackgroundTaskRegistration taskReg = authTaskBuilder.Register(); BackgroundTaskBuilder plugTaskBuilder = new BackgroundTaskBuilder(); plugTaskBuilder.Name = authBGTaskName; plugTaskBuilder.TaskEntryPoint = authBGTaskEntryPoint; plugTaskBuilder.SetTrigger(deviceWatcherTrigger); BackgroundTaskRegistration taskReg2 = plugTaskBuilder.Register(); //String taskRegName = taskReg.Name; //BackgroundTaskBuilder rebootTaskBuilder = new BackgroundTaskBuilder(); //rebootTaskBuilder.Name = authBGTaskName; //rebootTaskBuilder.TaskEntryPoint = authBGTaskEntryPoint; //SystemTrigger trigger = new SystemTrigger(SystemTriggerType.UserPresent, false); //rebootTaskBuilder.SetTrigger(trigger); //BackgroundTaskRegistration taskReg3 = rebootTaskBuilder.Register(); //String taskRegName = taskReg.Name; //taskReg.Progress += OnBgTaskProgress; System.Diagnostics.Debug.WriteLine("[RegisterTask] Background task registration is completed."); taskRegistered = true; } } }
private void StartWatcher() { DeviceWatcherEventKind[] triggerEventKinds = { DeviceWatcherEventKind.Add, DeviceWatcherEventKind.Remove /*, DeviceWatcherEventKind.Update */ }; //IEnumerable<DeviceWatcherEventKind> triggerEventKinds = DeviceWatcherEventKind.Add; DeviceWatcher deviceWatcher = null; string selector = SmartCardReader.GetDeviceSelector(); selector += " AND System.Devices.DeviceInstanceId:~~\"Ledger\""; deviceWatcher = DeviceInformation.CreateWatcher(selector, null); DeviceWatcherTrigger deviceWatcherTrigger = deviceWatcher.GetBackgroundTrigger(triggerEventKinds); RegisterTask(deviceWatcherTrigger); }
private void StartWatcher() { DeviceWatcherEventKind[] triggerEventKinds = { DeviceWatcherEventKind.Add, DeviceWatcherEventKind.Remove, DeviceWatcherEventKind.Update }; DeviceWatcher deviceWatcher = null; startWatcherButton.IsEnabled = false; // First get the device selector chosen by the UI. DeviceSelectorInfo deviceSelectorInfo = (DeviceSelectorInfo)selectorComboBox.SelectedItem; if (null == deviceSelectorInfo.Selector) { // If the a pre-canned device class selector was chosen, call the DeviceClass overload deviceWatcher = DeviceInformation.CreateWatcher(deviceSelectorInfo.DeviceClassSelector); } else if (deviceSelectorInfo.Kind == DeviceInformationKind.Unknown) { // Use AQS string selector from dynamic call to a device api's GetDeviceSelector call // Kind will be determined by the selector deviceWatcher = DeviceInformation.CreateWatcher( deviceSelectorInfo.Selector, null // don't request additional properties for this sample ); } else { // Kind is specified in the selector info deviceWatcher = DeviceInformation.CreateWatcher( deviceSelectorInfo.Selector, null, // don't request additional properties for this sample deviceSelectorInfo.Kind); } // Get the background trigger for this watcher DeviceWatcherTrigger deviceWatcherTrigger = deviceWatcher.GetBackgroundTrigger(triggerEventKinds); rootPage.NotifyUser("Starting Watcher...", NotifyType.StatusMessage); // Register this trigger for our background task RegisterBackgroundTask(deviceWatcherTrigger); stopWatcherButton.IsEnabled = true; rootPage.NotifyUser("Watcher started...", NotifyType.StatusMessage); }
private void RegisterBackgroundTask(DeviceWatcherTrigger deviceWatcherTrigger) { BackgroundTaskBuilder taskBuilder; // First see if we already have this background task registered. If so, unregister // it before we register it with the new trigger. foreach (var task in BackgroundTaskRegistration.AllTasks) { if (backgroundTaskName == task.Value.Name) { UnregisterBackgroundTask(task.Value); } } taskBuilder = new BackgroundTaskBuilder(); taskBuilder.Name = backgroundTaskName; taskBuilder.TaskEntryPoint = "BackgroundDeviceWatcherTaskCs.BackgroundDeviceWatcher"; taskBuilder.SetTrigger(deviceWatcherTrigger); backgroundTaskRegistration = taskBuilder.Register(); backgroundTaskRegistration.Completed += new BackgroundTaskCompletedEventHandler(OnTaskCompleted); }