Exemplo n.º 1
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() => {
                try
                {
                    var counter = new TaskCounter();
                    counter.RunCounter(_cts.Token).Wait();
                }
                catch (Android.OS.OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(() => MessagingCenter.Instance.Send(message, "CancelledMessage"));
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
Exemplo n.º 2
0
 public async Task Start()
 {
     _cts    = new CancellationTokenSource();
     _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);
     try
     {
         var counter = new TaskCounter();
         await counter.RunCounter(_cts.Token);
     }
     catch (OperationCanceledException)
     {
     }
     finally
     {
         if (_cts.IsCancellationRequested)
         {
             var message = new CancelledMessage();
             Device.BeginInvokeOnMainThread(() => MessagingCenter.Instance.Send(message, "CancelledMessage"));
         }
     }
     UIApplication.SharedApplication.EndBackgroundTask(_taskId);
 }