コード例 #1
0
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            PrimesInput input = (PrimesInput)e.Argument;

            int[] primes = Worker.FindPrimes(input, backgroundWorker);
            if (backgroundWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            e.Result = primes;
        }
コード例 #2
0
        private void bSubmit_Click(object sender, RoutedEventArgs e)
        {
            bSubmit.IsEnabled = false;
            bCancel.IsEnabled = true;
            Result.Items.Clear();
            int from, to;

            try
            {
                from = Int32.Parse(From.Text.Trim());
                to   = Int32.Parse(To.Text.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show("В полях для ввода некорректные значения");
                return;
            }
            PrimesInput input = new PrimesInput(from, to);

            backgroundWorker.RunWorkerAsync(input);
        }
コード例 #3
0
        static public int[] FindPrimes(PrimesInput input, BackgroundWorker backgroundWorker)
        {
            int num = input.from;

            int[] arr       = new int[input.to - input.from];
            int   iteration = arr.Length / 100;

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = num;
                num++;
            }
            int maxDiv = (int)Math.Floor(Math.Sqrt(input.to));

            int[] marks = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 2; j <= maxDiv; j++)
                {
                    if ((arr[i] != j) && (arr[i] % j == 0))
                    {
                        marks[i] = 1;
                    }
                }
                if ((i % iteration == 0) && (backgroundWorker != null))
                {
                    if (backgroundWorker.CancellationPending)
                    {
                        return(null);
                    }
                    if (backgroundWorker.WorkerReportsProgress)
                    {
                        backgroundWorker.ReportProgress(i / iteration);
                    }
                }
            }
            int primes = 0;

            for (int i = 0; i < marks.Length; i++)
            {
                if (marks[i] == 0)
                {
                    primes++;
                }
            }
            int[] ret  = new int[primes];
            int   curs = 0;

            for (int i = 0; i < marks.Length; i++)
            {
                if (marks[i] == 0)
                {
                    ret[curs] = arr[i];
                    curs++;
                }
            }
            if (backgroundWorker != null && backgroundWorker.WorkerReportsProgress)
            {
                backgroundWorker.ReportProgress(100);
            }
            return(ret);
        }
コード例 #4
0
 static public int[] FindPrimes(PrimesInput input)
 {
     return(FindPrimes(input, null));
 }