public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { _cts = new CancellationTokenSource(); Task.Run(() => { try { var refreshTask = new RefreshWeatherTaskService(); refreshTask.RunTask(_cts.Token).Wait(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e); } finally { if (_cts.IsCancellationRequested) { Device.BeginInvokeOnMainThread( () => MessagingCenter.Send(new StopRefreshWeather(), typeof(StopRefreshWeather).FullName) ); } } }, _cts.Token); return(StartCommandResult.Sticky); }
public async Task Start() { _cts = new CancellationTokenSource(); _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration); try { var refreshTask = new RefreshWeatherTaskService(); await refreshTask.RunTask(_cts.Token); } catch (Exception e) { } finally { if (_cts.IsCancellationRequested) { Device.BeginInvokeOnMainThread( () => MessagingCenter.Send(new StopRefreshWeather(), typeof(StopRefreshWeather).FullName) ); } } UIApplication.SharedApplication.EndBackgroundTask(_taskId); }