Ejemplo n.º 1
0
        private async void CalculateButton_Click(object sender, EventArgs e)
        {
            _source = new CancellationTokenSource();
            CancellationToken token = _source.Token;

            if ((FromTextBox.Text != "") && (ToTextBox.Text != ""))
            {
                CalculateButton.Enabled = false;
                CancelButton.Enabled    = true;

                var countPrimes = await PrimeFinder.CountPrimesAsync(int.Parse(FromTextBox.Text), int.Parse(ToTextBox.Text),
                                                                     token.WaitHandle);

                CountLabel.Text = string.Format($"Number of primes in range: {countPrimes}.");

                if (SaveLocationTextBox.Text != "")
                {
                    var saveLocation = string.Format(SaveLocationTextBox.Text + ".txt");
                    using (var writer = new StreamWriter(saveLocation, true))
                    {
                        writer.WriteLine($"Calculation done on: {DateTime.Now}. Number of primes in range {FromTextBox.Text} to {ToTextBox.Text}: {countPrimes}.");
                    }
                    SaveLocationTextBox.Text = string.Format($"Calculation saved to file: \"{saveLocation}\"");
                }

                CancelButton.Enabled    = false;
                CalculateButton.Enabled = true;

                _source.Dispose();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns an integer counting the number of prime numbers between input 1 and input 2 of user.
        /// </summary>
        /// <param name="input1"></param>
        /// <param name="input2"></param>
        /// <param name="waitHandle"></param>
        /// <returns></returns>
        public static int CountPrimes(int input1, int input2, WaitHandle waitHandle)
        {
            var primeList = new ArrayList();

            for (var i = Math.Min(input1, input2); i <= Math.Max(input1, input2); i++)
            {
                if (waitHandle != null)
                {
                    if (waitHandle.WaitOne(0))
                    {
                        break;
                    }
                }

                if (PrimeFinder.CheckPrime(i))
                {
                    primeList.Add(i);
                }
            }

            return(primeList.Count);
        }
Ejemplo n.º 3
0
        private void CalculateButton_Click(object sender, EventArgs e)
        {
            _source = new CancellationTokenSource();
            CancellationToken token = _source.Token;

            Task.Run(() =>
            {
                if ((FromTextBox.Text != "") && (ToTextBox.Text != ""))
                {
                    CalculateButton.Enabled = false;
                    CancelButton.Enabled    = true;

                    var resultingPrimes      = PrimeFinder.CalcPrimes(int.Parse(FromTextBox.Text), int.Parse(ToTextBox.Text), token.WaitHandle);
                    ResultListBox.DataSource = resultingPrimes;

                    CancelButton.Enabled    = false;
                    CalculateButton.Enabled = true;

                    _source.Dispose();
                }
            }, token);
        }