Ejemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            bool resultParse = int.TryParse(TheControl.Number, out int number);

            if (resultParse)
            {
                PgbEvolution.Minimum = 0;
                PgbEvolution.Maximum = 100;
                //bgw.RunWorkerAsync(number);

                Task myTask = Task.Run(() =>
                {
                    myFact              = new Factorial();
                    myFact.Progressing += MyFact_Progressing;
                    myFact.Completed   += MyFact_RunWorkerComplete;
                    BigInteger result   = myFact.CalculateAsync(number);
                    if (myFact.CancelCalculate) //,CancellationToken myToken
                    {
                        //e.Cancel = true;
                    }
                });
            }
        }
Ejemplo n.º 2
0
        /*private void Bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
         * {
         *  PgbEvolution.Value = e.ProgressPercentage;
         * }*/

        /*private void Bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
         * {
         *  if (e.Cancelled)
         *  {
         *      TxtMessage.Text = "Thread cancelled";
         *      TxtResult.Text = string.Empty;
         *  }
         *  else if (e.Error != null)
         *  {
         *      TxtMessage.Text = e.Error.ToString();
         *      TxtResult.Text = "";
         *  }
         *  else
         *  {
         *      TxtResult.Text = e.Result.ToString();
         *  }
         *
         * }*/

        /*private void Bgw_DoWork(object sender, DoWorkEventArgs e)
         * {
         *
         * myFact = new Factorial();
         * myFact.Progressing += MyFact_Progressing;
         * BigInteger result = myFact.Calculate((int)e.Argument);
         * if (myFact.CancelCalculate)
         * {
         *  e.Cancel = true;
         * }
         *
         * e.Result = result;
         *
         * }*/

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            bool resultParse = int.TryParse(TheControl.Number, out int number);

            if (resultParse)
            {
                CTS = new CancellationTokenSource();
                Button myButton = (Button)sender;
                myButton.IsEnabled  = false;
                BtnCancel.IsEnabled = true;

                PgbEvolution.Minimum = 0;
                PgbEvolution.Maximum = 100;
                //bgw.RunWorkerAsync(number);
                myFact              = new Factorial();
                myFact.Progressing += MyFact_Progressing;
                myFact.Completed   += MyFact_Completed;
                myFact.Cancelled   += MyFact_Cancelled;

                Task t = new Task <BigInteger>(() => myFact.CalculateAsync(number, CTS.Token));
                t.Start();
            }
        }