Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        private void ScheduleNext()
        {
            try
            {
                Task.Run(async() => await _backgroundService.PeriodicTaskAsync()).Wait();
            }
            catch (Exception e)
            {
                Android.Util.Log.Error(_serviceName, e.ToString());
            }

            _handler?.PostDelayed(ScheduleNext,
                                  (long)_backgroundService.PeriodicServiceCallInterval.TotalMilliseconds);
        }