private async Task StartInBackground() { try { _backgroundService = _backgroundServiceCreationFunc(); await _backgroundService.StartAsync(); using (var powerService = ApplicationContext.GetSystemService(PowerService) as PowerManager) { if (powerService == null) { return; } _wakeLock = powerService.NewWakeLock(WakeLockFlags.Partial, _serviceName); } AcquireWakelock(); if (_hasPeriodicTask) { StartHandlerThread(); } } catch (Exception e) { Android.Util.Log.Error(_serviceName, e.ToString()); } }
private async Task BackgroundTaskAsync() { try { _backgroundService = _backgroundServiceCreationFunc(); await _backgroundService.StartAsync(); _messagingCenter.Send <object, BackgroundServiceState>(this, FromBackgroundMessages.BackgroundServiceState, new BackgroundServiceState(true)); while (!_cancellationTokenSource.IsCancellationRequested) { try { await _backgroundService.PeriodicTaskAsync(); if (!_cancellationTokenSource.IsCancellationRequested) { await Task.Delay(_backgroundService.PeriodicServiceCallInterval, _cancellationTokenSource.Token); } } catch (OperationCanceledException) { } } } catch (Exception e) { Debug.WriteLine(e); } }
private async Task StartAsync() { try { await _backgroundService.StartAsync(_cancellationTokenSource.Token); var periodicServices = _backgroundService.GetPeriodicServices(); foreach (var(periodicService, period) in periodicServices) { Debug.WriteLine($"Periodic service starting: {periodicService.Name}"); _ = Task.Run( async() => await PeriodicTaskWrapper(periodicService, period, _cancellationTokenSource.Token), _cancellationTokenSource.Token) .ConfigureAwait(false); Debug.WriteLine($"Periodic service started: {periodicService.Name}"); } } catch (Exception e) { Debug.WriteLine(e); } }
private async Task StartInBackground() { try { _cancellationTokenSource = new CancellationTokenSource(); await _backgroundService.StartAsync(_cancellationTokenSource.Token); using (var powerService = ApplicationContext.GetSystemService(PowerService) as PowerManager) { if (powerService == null) { return; } _wakeLock = powerService.NewWakeLock(WakeLockFlags.Partial, _builder.ServiceName); } AcquireWakeLock(); StartPeriodicTasks(_cancellationTokenSource.Token); } catch (Exception e) { Log.Error(_builder.ServiceName, e.ToString()); } }