Esempio n. 1
0
        public async Task RunCounter(CancellationToken token)
        {
            try
            {
                await Task.Run(async() => {
                    for (long i = 0; i < long.MaxValue; i++)
                    {
                        token.ThrowIfCancellationRequested();

                        await Task.Delay(5000);
                        var message = new TickedMessage
                        {
                            Message = i.ToString()
                        };

                        AvisarSoyLento(Vehicle.Otro);

                        Device.BeginInvokeOnMainThread(() => {
                            MessagingCenter.Send <TickedMessage>(message, "TickedMessage");
                        });
                    }
                }, token);
            }
            catch (Exception ex)
            {
                RunCounter(token);
            }
        }
Esempio n. 2
0
        public async Task RunBackgroundTask(CancellationToken token)
        {
            //var locales = await CrossTextToSpeech.Current.GetInstalledLanguages();
            string entry = "Unknown";

            await Task.Run(async() =>
            {
                for (long i = 0; i < long.MaxValue; i++)
                {
                    token.ThrowIfCancellationRequested();

                    await Task.Delay(100); //100ms

                    switch (_entered)
                    {
                    case true:
                        entry = "Entered";
                        break;

                    case false:
                        entry = "Exited";
                        break;
                    }

                    var message = new TickedMessage
                    {
                        Message = "Current status: " + entry
                    };
                    if (_entered == null)
                    {
                        message = new TickedMessage
                        {
                            Message = "Failed to obtain status."
                        };
                    }

                    //forces the background proces to update the current coordinates
                    if (i % 10 == 0)
                    {
                        //await CheckAvailability(); //used for debugging
                        await CheckEntry();
                    }

                    Device.BeginInvokeOnMainThread(() => { MessagingCenter.Send(message, "TickedMessage"); });
                }
            }, token);
        }
        public async Task RunCounter(CancellationToken token)
        {
            await Task.Run(async() => {
                for (long i = 0; i < long.MaxValue; i++)
                {
                    token.ThrowIfCancellationRequested();

                    await Task.Delay(250);
                    var message = new TickedMessage {
                        Message = i.ToString()
                    };

                    Device.BeginInvokeOnMainThread(() => {
                        MessagingCenter.Send <TickedMessage>(message, "TickedMessage");
                    });
                }
            }, token);
        }
Esempio n. 4
0
        public async Task Start(CancellationToken token)
        {
            run = true;
            await Task.Run(async() => {
                while (run)
                {
                    token.ThrowIfCancellationRequested();

                    await Task.Delay(1000);
                    var message = new TickedMessage();

                    Device.BeginInvokeOnMainThread(() => {
                        MessagingCenter.Send <TickedMessage>(message, "TickedMessage");
                    });
                }
            }
                           , token);
        }
        public async Task RunCounter(CancellationToken token)
        {
            await Task.Run(async() =>
            {
                for (int i = 0; i < long.MaxValue; i++)
                {
                    token.ThrowIfCancellationRequested();

                    await Task.Delay(TimeSpan.FromSeconds(1));

                    var message = new TickedMessage()
                    {
                        Message = i.ToString()
                    };

                    Device.BeginInvokeOnMainThread(() =>
                    {
                        MessagingCenter.Send(message, MessagingNames.TickedMessage);
                    });
                }
            });
        }
        public async Task EjecutarSincronizacionAutomatica(System.Threading.CancellationToken token)
        {
            bool               ejecutarNuevamente = true;
            TimeSpan           frecuenciaEjecucionSincronizacion = TimeSpan.FromMinutes(5);
            EventoLogisticoBLL eventosBLL = new EventoLogisticoBLL();
            var eventosPendientes         = eventosBLL.SeleccionarEventosPendientesSincronizar();

            if (eventosPendientes != null && eventosPendientes.Count > 0)
            {
                ejecutarNuevamente = true;
            }
            else
            {
                ejecutarNuevamente = false;
            }

            while (ejecutarNuevamente)
            {
                token.ThrowIfCancellationRequested();

                if (await Common.ParametrosSistema.isOnline)
                {
                    eventosPendientes = eventosBLL.SeleccionarEventosPendientesSincronizar();
                    if (eventosPendientes != null && eventosPendientes.Count > 0)
                    {
                        int totalRegistrosSincronizados = 0;
                        try
                        {
                            List <RespuestaProcesoEventoLogistico> respuestas = new List <RespuestaProcesoEventoLogistico>();

                            foreach (EventoLogistico evento in eventosPendientes)
                            {
                                token.ThrowIfCancellationRequested();
                                respuestas.Add(await eventosBLL.GuardarEventoLogistico(evento));
                                totalRegistrosSincronizados++;
                                var message = new TickedMessage
                                {
                                    Message = ((decimal)totalRegistrosSincronizados / (decimal)eventosPendientes.Count).ToString()
                                };

                                Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                                {
                                    Xamarin.Forms.MessagingCenter.Send <TickedMessage>(message, "TickedMessage");
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                            if (totalRegistrosSincronizados == eventosPendientes.Count)
                            {
                                ejecutarNuevamente = false;
                            }
                            else
                            {
                                ejecutarNuevamente = false;
                            }
                        }
                        Xamarin.Forms.DependencyService.Get <INotificationService>().ShowLocalNotification("Sincronización automática", "Se han sincronizado " + eventosPendientes.Count + " registros.", DateTime.Now);
                    }
                    else
                    {
                        //Si no existen registros pendientes por sincronizar se detiene la ejecución de esta función
                        ejecutarNuevamente = false;
                    }
                }
                else
                {
                    ejecutarNuevamente = true;
                }
                await Task.Delay(Convert.ToInt32(frecuenciaEjecucionSincronizacion.TotalMilliseconds));
            }
        }
        public async Task RunCounter(CancellationToken token)
        {
            int i = 0;

            stopwatch       = new Stopwatch();
            servicePosition = new PositionService();
            IConnection     = new CheckInternetConnection();

            int?   minPosAnterior     = null;
            string msgPos             = string.Empty;
            String msgWithOutInternet = string.Empty;

            await Task.Run(async() => {
                while (true)
                {
                    token.ThrowIfCancellationRequested();

                    if (!stopwatch.IsRunning)
                    {
                        stopwatch.Start();
                        Device.StartTimer(TimeSpan.FromMilliseconds(100), () =>
                        {
                            TimeSpan ts  = stopwatch.Elapsed;
                            String timer = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);

                            var tmessage = "TickedMessage";
                            if (token.IsCancellationRequested == true)
                            {
                                stopwatch.Stop();
                                timer = "00:00:00";
                                stopwatch.Reset();
                                tmessage = "CancelledMessage";
                            }

                            var message     = new TickedMessage();
                            message.Message = "|" + timer + "|";
                            if (minPosAnterior != null && minPosAnterior != ts.Minutes)
                            {
                                Boolean HasInternet = IConnection.HasInternetConnection();

                                if (HasInternet)
                                {
                                    i++;
                                    //Save position
                                    Task taskPosition = new Task(() => servicePosition.SetPosition());
                                    taskPosition.Start();

                                    msgWithOutInternet = string.Empty;
                                }
                                else
                                {
                                    msgWithOutInternet = "Rastreamento não está sendo realizado por falta de internet.";
                                }

                                if (i == 1)
                                {
                                    msgPos = " posição rastreada.";
                                }
                                else
                                {
                                    msgPos = " posições rastreadas.";
                                }
                            }

                            message.Message = i.ToString() + msgPos + "|" + timer + "|" + msgWithOutInternet;

                            minPosAnterior = ts.Minutes;

                            Device.BeginInvokeOnMainThread(() =>
                            {
                                MessagingCenter.Send <TickedMessage>(message, tmessage);
                            });

                            if (!stopwatch.IsRunning)
                            {
                                return(false);
                            }
                            { return(true); }
                        });
                    }

                    //return false;
                }
            }, token);
        }
Esempio n. 8
0
        public async Task StartListening(CancellationToken token)
        {
            await Task.Run(async() =>
            {
                //if (!JourneyOngoing)
                //{
                //    if (Settings.Address != "" && Settings.Longitude != "" && Settings.Latitude != "")
                //    {
                //        ChosenLocation.Longitude = Double.Parse(Settings.Longitude);
                //        ChosenLocation.Latitude = Double.Parse(Settings.Latitude);
                //        ChosenLocation.Address = Settings.Address;

                //        var locator = CrossGeolocator.Current;
                //        locator.PositionChanged += Locator_PositionChanged;

                //        if (!CrossGeolocator.Current.IsListening)
                //        {
                //            await locator.StartListeningAsync(new TimeSpan(1), .1);
                //        }
                //        JourneyOngoing = true;
                //        int timeCounter = 0;
                //        var tickedMessage = new TickedMessage
                //        {
                //            TimeTick = timeCounter
                //        };
                //        Device.StartTimer(TimeSpan.FromSeconds(1), () =>
                //        {
                //            token.ThrowIfCancellationRequested();

                //            if (JourneyOngoing)
                //            {
                //                //send tick
                //                Device.BeginInvokeOnMainThread(() => {
                //                    MessagingCenter.Send<TickedMessage>(tickedMessage, "TickedMessage");
                //                });
                //                return true;
                //            }

                //            return false;

                //        });

                //    }
                //}
                //else
                //{
                //    CrossToastPopUp.Current.ShowToastMessage("Set the location address to continue");
                //    await Application.Current.MainPage.Navigation.PushAsync(new SearchPage());
                //}

                var tickedMessage = new TickedMessage
                {
                    TimeTick = 0
                };
                var runOnce = false;
                while (true)
                {
                    token.ThrowIfCancellationRequested();
                    if (!runOnce)
                    {
                        Device.BeginInvokeOnMainThread(() => {
                            MessagingCenter.Send <TickedMessage>(tickedMessage, "StartJourneyMessage");
                        });
                        runOnce = true;
                    }
                }
            }, token);
        }