private async void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { await SharedState.LogAsync($"BackgroundScanner ({sender.InstanceId}): Background task cancelled. Reason: {reason}"); _taskInstance.Canceled -= TaskInstance_Canceled; _taskInstance.Progress = 0; _deferral.Complete(); }
private async void OnResuming(object sender, object e) { await SharedState.LogAsync("Scannit: RESUMING!"); await SharedState.SetAsync(SharedState.IsApplicationInForeground, true); await Scanner.StartScanner(); }
public async void Run(IBackgroundTaskInstance taskInstance) { await SharedState.LogAsync($"BackgroundScanner ({taskInstance.InstanceId}): Starting run of background task..."); _taskInstance = taskInstance; _deferral = taskInstance.GetDeferral(); _taskInstance.Progress = 1; _taskInstance.Canceled += TaskInstance_Canceled; string selector = SmartCardReader.GetDeviceSelector(); DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector); var reader = await SmartCardReader.FromIdAsync(devices.FirstOrDefault().Id); await SharedState.LogAsync($"BackgroundScanner ({taskInstance.InstanceId}): Got card reader device."); reader.CardAdded += Reader_CardAdded; }
private async void Reader_CardAdded(SmartCardReader sender, CardAddedEventArgs args) { await SharedState.LogAsync($"BackgroundScanner ({_taskInstance.InstanceId}): CardAdded event fired."); try { TravelCard card = await CardOperations.ReadTravelCardAsync(args.SmartCard); if (card != null) { await SharedState.LogAsync($"BackgroundScanner ({_taskInstance.InstanceId}): Successful read card."); Task updateCardTask = SharedState.SetAsync(SharedState.LastSeenCard, card.RawValues); await SharedState.LogAsync($"BackgroundScanner ({_taskInstance.InstanceId}): LastSeenCard updated."); Task updateTimestampTask = SharedState.SetAsync(SharedState.LastSeenTimestamp, DateTimeOffset.UtcNow); await SharedState.LogAsync($"BackgroundScanner ({_taskInstance.InstanceId}): LastSeenTimestamp updated."); if (await SharedState.GetAsync <bool>(SharedState.IsApplicationInForeground)) { await SharedState.LogAsync($"BackgroundScanner ({_taskInstance.InstanceId}): Application is in the foreground. Changed Progress value."); _taskInstance.Progress = 2; } else { await SharedState.LogAsync($"BackgroundScanner ({_taskInstance.InstanceId}): Application is in the background. Post toast notification."); PostToastNotification(card); } } } catch (Exception ex) { await SharedState.LogAsync($"BackgroundScanner ({_taskInstance.InstanceId}): Failed to read travel card! Exception: {ex}\nStack trace: {ex.StackTrace}"); } }
private async void OnSuspending(object sender, SuspendingEventArgs e) { SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral(); var newSession = new ExtendedExecutionSession { Reason = ExtendedExecutionReason.SavingData }; newSession.Revoked += NewSession_Revoked; ExtendedExecutionResult result = await newSession.RequestExtensionAsync(); if (result == ExtendedExecutionResult.Allowed) { await SharedState.LogAsync("Scannit: SUSPENDING!"); await SharedState.SetAsync(SharedState.IsApplicationInForeground, false); newSession.Revoked -= NewSession_Revoked; newSession.Dispose(); newSession = null; } deferral.Complete(); }