コード例 #1
0
        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);

                if (longTask != null)
                {
                    await longTask.Run(_cts.Token);
                }

                //await Task.Run(() => {

                //    _cts.Token.ThrowIfCancellationRequested();

                //    LongTask.Start();

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

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

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
コード例 #2
0
        protected override void OnHandleWork(Intent p0)
        {
            var parcelable = (UploadLongRunningTaskParcelable)p0.GetParcelableExtra("task");

            longTask = parcelable.Task;

            _cts = new CancellationTokenSource();

            Task.Run(async() =>
            {
                try
                {
                    //INVOKE THE SHARED CODE
                    //var counter = new TaskCounter();
                    //counter.RunCounter(_cts.Token).Wait();
                    if (longTask != null)
                    {
                        await longTask.Run(_cts.Token);
                    }
                }
                catch (System.OperationCanceledException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        //var message = new CancelledMessage();
                        //Device.BeginInvokeOnMainThread(
                        //    () => MessagingCenter.Send(message, "CancelledMessage")
                        //);
                    }
                }
            }, _cts.Token);
        }