示例#1
0
        /// <summary>
        /// Stop the native background service host
        /// </summary>
        public async void Stop()
        {
            if (!IsStarted)
            {
                return;
            }
            IsStarted = false;
            _cancellationTokenSource?.Cancel();
            using (var mre = new ManualResetEventSlim())
            {
                await Task.Run(async() =>
                {
                    await _backgroundService.StopAsync();
                    mre.Set();
                });

                if (!mre.Wait(_cleanupTimeout))
                {
                    Debug.WriteLine("Stopping background service timed out.");
                }
            }

            _messagingCenter.Send <object, BackgroundServiceState>(this, FromBackgroundMessages.BackgroundServiceState, new BackgroundServiceState(false));

            _cancellationTokenSource?.Dispose();
        }
        private async void Cleanup()
        {
            StopPeriodicTasks();

            using (var mre = new ManualResetEventSlim())
            {
                await Task.Run(async() =>
                {
                    _cancellationTokenSource.Cancel();
                    await _backgroundService.StopAsync();
                    mre.Set();
                });

                if (!mre.Wait(_cleanupTimeout))
                {
                    Log.Error(_builder.ServiceName, "Background service did not cleanup within timeout time");
                }
            }

            ReleaseWakeLock();
            StopForeground(true);
            StopSelf();
            IsStarted = false;
            _messagingCenter.Send <object, BackgroundServiceState>(this,
                                                                   FromBackgroundMessages.BackgroundServiceState, new BackgroundServiceState(false));
            _messagingCenter.Unsubscribe <object, UpdateNotificationMessage>(this, ToBackgroundMessages.UpdateBackgroundServiceNotificationMessage);
        }
示例#3
0
 /// <summary>
 /// Stop the native background service host
 /// </summary>
 public void Stop()
 {
     if (!IsStarted)
     {
         return;
     }
     IsStarted = false;
     //_locationManager.StopUpdatingLocation();
     //_locationManager.LocationsUpdated -= OnLocationsUpdated;
     //_locationManager.LocationUpdatesResumed -= OnLocationUpdatesResumed;
     //_locationManager.LocationUpdatesPaused -= OnLocationUpdatesPaused;
     _cancellationTokenSource?.Cancel();
     _cancellationTokenSource?.Dispose();
     _backgroundService.StopAsync()
     .ContinueWith(task => _messagingCenter.Send <object, BackgroundServiceState>(this, FromBackgroundMessages.BackgroundServiceState, new BackgroundServiceState(false)));
 }
示例#4
0
 private Task Cleanup()
 {
     return(_backgroundService?.StopAsync()
            .ContinueWith(task =>
     {
         if (_hasPeriodicTask)
         {
             StopHandlerThread();
         }
         ReleaseWakeLock();
         StopForeground(true);
         StopSelf();
         IsStarted = false;
         _messagingCenter.Send <object, BackgroundServiceState>(this,
                                                                FromBackgroundMessages.BackgroundServiceState, new BackgroundServiceState(false));
         _messagingCenter.Unsubscribe <object, UpdateNotificationMessage>(this, ToBackgroundMessages.UpdateBackgroundServiceNotificationMessage);
     }));
 }