Ejemplo n.º 1
0
        /// <summary>
        /// Gets called when the button to calculate Pi is pressed.
        /// </summary>
        /// <param name="sender">Sender</param>
        async partial void HandleCalculatePi(UIButton sender)
        {
            // Toggle status.
            if (this.IsCalculating)
            {
                this.IsCalculating = false;
                btnCalculate.SetTitle("Start calculating", UIControlState.Normal);

                // TODO: Cancel calculation if it is currently running.
                this.cts.Cancel();
                return;
            }

            this.IsCalculating = true;
            btnCalculate.SetTitle("Stop calculating", UIControlState.Normal);
            this.txtPi.Text = string.Empty;


            // TODO: Reset cancellation token source.
            this.cts = new CancellationTokenSource();

            // Start a background task to request more time from the operating system.
            // This has no negative side effect if called while the app is in the foreground.
            nint finiteTaskId = UIApplication.SharedApplication.BeginBackgroundTask(this.HandleBackgroundTimeExpires);

            // Now start calculating on a separate thread.
            // TODO: Pass token to Pi calculation and to the Task.Run() call.
            try
            {
                await Task.Run(() =>
                               PiHelper.CalcPi(pieceOfPi => this.UpdateUi(pieceOfPi), this.cts.Token), // pass to UpdateUi()
                               this.cts.Token);                                                        // pass to Task.Run()
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Calculation canceled.");
                this.IsCalculating = false;
            }

            // Also end the background task.
            UIApplication.SharedApplication.EndBackgroundTask(finiteTaskId);
            finiteTaskId = -1;

            // Update the button's title.
            btnCalculate.SetTitle("Start calculating", UIControlState.Normal);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets called when the button to calculate Pi is pressed.
        /// </summary>
        /// <param name="sender">Sender</param>
        async partial void HandleCalculatePi(UIButton sender)
        {
            // Toggle status.
            if (this.IsCalculating)
            {
                this.IsCalculating = false;
                btnCalculate.SetTitle("Start calculating", UIControlState.Normal);
                this.cts.Cancel();
                return;
            }

            this.IsCalculating = true;
            btnCalculate.SetTitle("Stop calculating", UIControlState.Normal);
            this.txtPi.Text = string.Empty;
            this.cts        = new CancellationTokenSource();

            // Start a background task to request more time from the operating system.
            // This has no negative side effect if called while the app is in the foreground.
            nint finiteTaskId = UIApplication.SharedApplication.BeginBackgroundTask(this.HandleBackgroundTimeExpires);

            Console.WriteLine("Started background task: " + finiteTaskId);

            // Now start calculating on a separate thread. Note that the cancellation token is passed to the Task.Run() method:
            // if the token is already cancelled, the task won't even start.
            try
            {
                await Task.Run(() =>
                               PiHelper.CalcPi(pieceOfPi => this.UpdateUi(pieceOfPi), this.cts.Token),
                               this.cts.Token);
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Calculation cancelled.");
                this.IsCalculating = false;
            }

            // Also end the background task.
            Console.WriteLine("Ending background task: " + finiteTaskId);
            UIApplication.SharedApplication.EndBackgroundTask(finiteTaskId);
            finiteTaskId = -1;

            // Update the button's title.
            btnCalculate.SetTitle("Start calculating", UIControlState.Normal);
        }
Ejemplo n.º 3
0
 public static void CalcPi(Action <string> piCallback, CancellationToken token = default(CancellationToken))
 {
     PiHelper.Process(1000000, piCallback, token);
 }