public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

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

            }, _cts.Token);

            return StartCommandResult.Sticky;
        }
예제 #2
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() => {
                try
                {
                    //INVOKE THE SHARED CODE
                    var b = App.ShouldRunOnSleep();
                    while (App.IsSleep && b)
                    {
                        Task.Delay(1000);
                        App.Locator.RunPauseSchedule.OneSecondBackgroundUpdate();
                    }
                }
                catch (System.OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
예제 #3
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

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

            return(StartCommandResult.Sticky);
        }
예제 #4
0
        public async Task Start()
        {
            _cts = new CancellationTokenSource();

            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            try
            {
                //INVOKE THE SHARED CODE
                var counterServiceMainLoop = new LongrunningTimerTask(App.counterService);
                await counterServiceMainLoop.CounterAdd(_cts.Token);
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "CancelledMessage")
                        );
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
예제 #5
0
 private void CollectionService()
 {
     Task.Run(() =>
     {
         try
         {
             if (!isRunning)
             {
                 var sen = new SensorCollectionManager();
                 //sen.PhoneSensorOperationsAsync(_cts.Token);
             }
         }
         catch (Exception ex)
         {
             ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString());
         }
         finally
         {
             if (_cts.IsCancellationRequested)
             {
                 var message = new CancelledMessage();
                 Device.BeginInvokeOnMainThread(
                     () => MessagingCenter.Send(message, "CancelledMessage")
                     );
             }
         }
     }, _cts.Token);
 }
예제 #6
0
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() =>
            {
                try
                {
                    var locationTask = new LocationTask();
                    locationTask.Run(_cts.Token).Wait();
                }
                catch (Android.OS.OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        MainThread.BeginInvokeOnMainThread(() => MessagingCenter.Send(message, nameof(CancelledMessage)));
                    }
                }
            });

            var notification = CreateNotification();

            StartForeground(1, notification);

            return(StartCommandResult.NotSticky);
        }
예제 #7
0
        public async Task Start()
        {
            _cts = new CancellationTokenSource();

            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            try
            {
                //INVOKE THE SHARED CODE
                var b = App.ShouldRunOnSleep();
                while (App.IsSleep && b)
                {
                    await Task.Delay(1000);

                    App.Locator.RunPauseSchedule.OneSecondBackgroundUpdate();
                }
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "CancelledMessage")
                        );
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
예제 #8
0
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] 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();
                        MainThread.BeginInvokeOnMainThread(() => MessagingCenter.Send(message, nameof(CancelledMessage)));
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() => {
                try
                {
                    //INVOKE THE SHARED CODE
                    var locationService = new LocationService();
                    locationService.GetUserLocationAsync(_cts.Token).Wait();
                }
                catch (OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() => {
                try
                {
                    //INVOKE THE SHARED CODE
                    var counter = new TaskCounter(Android.App.Application.Context);
                    counter.RunCounter(_cts.Token).Wait();
                }
                catch (OperationCanceledException e)
                {
                }

                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
예제 #11
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            Task.Run(() => {
                try
                {
                    PowerManager pm          = (PowerManager)GetSystemService(Context.PowerService);
                    PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "My Tag");
                    wl.Acquire();
                    var counter = new TimerTask();
                    counter.RunTimer(_cts, startId).Wait();
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        //Device.BeginInvokeOnMainThread(
                        //    () => MessagingCenter.Send(message, "CancelledMessage")
                        //);
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() =>
            {
                try
                {
                    //INVOKE THE SHARED CODE
                    var tmp           = FunctionPasserToNativeBackground.ClassObject;
                    Type type         = tmp.GetType();
                    MethodInfo method = type.GetMethod(FunctionPasserToNativeBackground.FunctionName);
                    //tmp.InitAlarmListRefresh(EventActions.subscribe);
                    method.Invoke(tmp, FunctionPasserToNativeBackground.Params.ToArray());
                }
                catch (Exception e)
                {
                    var x = e.StackTrace;
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(async() =>
            {
                try
                {
                    IDataUploader dataUploader = Startup.ServiceProvider?.GetService(typeof(IDataUploader)) as IDataUploader;
                    await dataUploader.StartQueuedUpdatesAsync(_cts.Token);
                    //await App.Current.UploadDataService.Instance.RunQueuedUpdatesAsync(_cts.Token);
                }
                catch (System.OperationCanceledException)
                {
                    //you could log this, or not
                    Analytics.TrackEvent("System.OperationCanceledException in DroidRunQueuedUpdateService");
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
예제 #14
0
        public async Task StartAsync()
        {
            _cts = new CancellationTokenSource();

            _taskId = UIApplication.SharedApplication.BeginBackgroundTask(OnExpiration);

            try
            {
                //if you don't run this method in an iOS "Background Task" and the user gets a phone call or does something which backgrounds the app,
                //iOS will give the app ~5 seconds (at the very most), to finish or iOS will terminate the app. BeginBackgroundTask allows the method to run for another
                //180 seconds. iOS fires off OnExpiration when the 180 is almost over and you need to cut the method off by cancelling it.
                await UploadDataService.Instance.RunQueuedUpdatesAsync(_cts.Token);
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(() => MessagingCenter.Send(message, "CancelledMessage"));
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
        public async Task Start()
        {
            cancellationTokenSource = new CancellationTokenSource();
            taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            try
            {
                // Shared code is invoked here
                var sender = new MobileApp.Services.BackgroundTaskService();
                await sender.ProcessAsync(cancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                // only used to stop execution
            }
            finally
            {
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "CancelledMessage")
                        );
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(taskId);
        }
        public async Task Start()
        {
            _cts = new CancellationTokenSource();

            _taskId = UIApplication.SharedApplication.BeginBackgroundTask(taskName, OnExpiration);

            try
            {
                var counter = new TaskCounter();
                await counter.RunCounter(_cts.Token);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        MessagingCenter.Send(message, MessagingNames.CancelledMessage);
                    });
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
예제 #17
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() => {
                try
                {
                    //INVOKE THE SHARED CODE
                    var counter = new TaskCounter();
                    counter.RunCounter(_cts.Token, intent.GetStringArrayListExtra("Barcode"), intent.GetStringExtra("PageName")).Wait();
                }
                catch (System.OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() => {
                try
                {
                    //INVOKE THE SHARED CODE
                    var journeyHelper = new JourneyHelper();
                    journeyHelper.StartListening(_cts.Token).Wait();
                }
                catch (System.OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "StopJourneyMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
예제 #19
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

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

            return(StartCommandResult.Sticky);
        }
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(async() =>
            {
                try
                {
                    var counter = new TaskCounter();
                    await counter.RunCounter(_cts.Token);
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            MessagingCenter.Send(message, MessagingNames.CancelledMessage);
                        });
                    }
                }
            });

            return(StartCommandResult.Sticky);
        }
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() => {
                try
                {
                    //INVOKE THE SHARED CODE
                    SincronizacionBLL sincronizacionBLL = new SincronizacionBLL();
                    sincronizacionBLL.EjecutarSincronizacionAutomatica(_cts.Token).Wait();
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
예제 #22
0
        public async Task Start()
        {
            _cts = new CancellationTokenSource();
            StaticObjects.IsCollecting = true;
            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            try
            {
                var net = new SensorCollectionManager();
                //await net.PhoneSensorOperationsAsync(_cts.Token);
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "CancelledMessage")
                        );
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
예제 #23
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() => {
                try
                {
                    // INVOKE THE SHARED CODE
                    var timerMainLoop = new LongrunningTimerTask(App.counterService);
                    timerMainLoop.CounterAdd(_cts.Token).Wait();
                }
                catch (Android.OS.OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
예제 #24
0
        public async Task Start()
        {
            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            _cts = new CancellationTokenSource();

            try
            {
                var counter = new TaskCounter();
                await counter.RunCounter(_cts.Token);
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    MainThread.BeginInvokeOnMainThread(() => MessagingCenter.Send(message, nameof(CancelledMessage)));
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(async() =>
            {
                try
                {
                    await UploadDataService.Instance.RunQueuedUpdatesAsync(_cts.Token);
                }
                catch (OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
        public async Task StartLongRunningTask(FunctionPassingToNativeBackground obj)
        {
            _cts = new CancellationTokenSource();

            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            try
            {
                //INVOKE THE SHARED CODE
                var        tmp    = obj.ClassObject;
                Type       type   = tmp.GetType();
                MethodInfo method = type.GetMethod(obj.FunctionName);
                await(Task) method.Invoke(tmp, obj.Params.ToArray());
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "CancelledMessage")
                        );
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
예제 #27
0
        public async Task Start(StartLongRunningTaskMessage startLongRunningTaskMessage)
        {
            _cts = new CancellationTokenSource();

            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            try
            {
                //INVOKE THE SHARED CODE
                var counter = new TaskCounter();
                await counter.RunCounter(_cts.Token, startLongRunningTaskMessage.Barcode, startLongRunningTaskMessage.PageName);
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "CancelledMessage")
                        );
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
        public async Task Start()
        {
            _cts = new CancellationTokenSource();

            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

            try
            {
                //INVOKE THE SHARED CODE
                var journeyHelper = new JourneyHelper();
                journeyHelper.StartListening(_cts.Token).Wait();
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new CancelledMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "StopJourneyMessage")
                        );
                }
            }

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
		public override StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
		{
			_cts = new CancellationTokenSource ();

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

			}, _cts.Token);

			return StartCommandResult.Sticky;
		}
예제 #30
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            cancellationTokenSource = new CancellationTokenSource();

            Task.Run(() =>
            {
                try
                {
                    // Shared code is invoked here
                    var sender = new MobileApp.Services.BackgroundTaskService();
                    sender.ProcessAsync(cancellationTokenSource.Token).Wait();
                }
                catch (OperationCanceledException)
                {
                }
                finally
                {
                    if (cancellationTokenSource.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            }, cancellationTokenSource.Token);

            return(StartCommandResult.Sticky);
        }
예제 #31
0
        public void StoreService()
        {
            Task.Run(() =>
            {
                try
                {
                    //INVOKE THE SHARED CODE
                    if (!isRunning)
                    {
                        var net = new NetworkCommunicationManager();
                        //net.DBOperations(_cts.Token);
                    }
                }
                catch (Exception ex)
                {
                    ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString());

                    var message = new CollectionMessage
                    {
                        Message = "Connection Error"
                    };
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "CollectionMessage")
                        );
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                    else
                    {
                        StaticObjects.IsCollecting = false;
                        //var _message = new StopLongRunningTaskMessage();
                        //MessagingCenter.Send(_message, "StopLongRunningTaskMessage");

                        var message = new CollectionMessage
                        {
                            Message = "Database push resuming..."
                        };
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CollectionMessage")
                            );
                    }
                }
            }, _cts.Token);
        }
예제 #32
0
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Task.Run(() =>
            {
                try
                {
                    var message = new LocationMessage();

                    message.Position = CrossGeolocator.Current.GetPositionAsync(System.TimeSpan.FromSeconds(5)).Result;

                    Device.BeginInvokeOnMainThread(() =>
                    {
                        MessagingCenter.Send <LocationMessage>(message, "LocationMessage");
                    });

                    Task.Run(async() =>
                    {
                        var locationTask = new LocationTask();

                        await locationTask.SendLocation(message.Position);
                    });

                    DisplayNotification($"{message.ToString()} às: {System.DateTime.Now.ToString("hh:mm:ss")}");
                    //DisplayNotification($"{message.Position.Latitude.ToString("N2")},{message.Position.Longitude.ToString("N2")} às: {System.DateTime.Now.ToString("hh:mm:ss")}");
                }
                catch (OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "CancelledMessage")
                            );
                    }
                }
            });

            return(StartCommandResult.Sticky);

            //return base.OnStartCommand(intent, flags, startId);
        }
		public async Task Start ()
		{
			_cts = new CancellationTokenSource ();

			_taskId = UIApplication.SharedApplication.BeginBackgroundTask ("LongRunningTask", OnExpiration);

			try {
				//INVOKE THE SHARED CODE
				var counter = new TaskCounter();
				await counter.RunCounter(_cts.Token);

			} catch (OperationCanceledException) {
			} finally {
				if (_cts.IsCancellationRequested) {
					var message = new CancelledMessage();
					Device.BeginInvokeOnMainThread (
						() => MessagingCenter.Send(message, "CancelledMessage")
					);
				}
			}

			UIApplication.SharedApplication.EndBackgroundTask (_taskId);
		}
예제 #34
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource ();

            Task.Run (() => {
                try {
                    var recentAds = new RecentAdsWebService();
                    recentAds.GetRecentAdsService(_cts.Token).Wait();
                }
                catch (OperationCanceledException) {
                }
                finally {
                    if (_cts.IsCancellationRequested) {
                        var message = new CancelledMessage();
                        Device.BeginInvokeOnMainThread (
                            () => MessagingCenter.Send(message, "CancelledMessage")
                        );
                    }
                }

            }, _cts.Token);

            return StartCommandResult.Sticky;
        }