Beispiel #1
0
        public static ConcurrentBag <int> CalcPrimes(int firstNum, int secondNum) //2.	Create a static method called CalcPrimes
        {
            var        resultList = new ConcurrentBag <int>();
            PrimeCheck primeCheck = new PrimeCheck();

            secondNum += 1;
            Random random = new Random();
            int    randomResult;

            if (primeCheck.isValidNumbers(firstNum, secondNum))
            {
                Parallel.For(firstNum, secondNum, (numberToCheck, loopState) =>
                {
                    randomResult = random.Next(10000000);
                    if (randomResult == 0)
                    {
                        loopState.Stop();
                    }
                    if (primeCheck.IsPrime(numberToCheck))
                    {
                        resultList.Add(numberToCheck);
                    }
                });
            }
            return(resultList);
        }
        public static IEnumerable <int> CalcPrimes(int firstNum, int secondNum, int maxDegree) //2.	Create a static method called CalcPrimes
        {
            var             resultList = new ConcurrentBag <int>();
            ParallelOptions op         = new ParallelOptions();

            op.MaxDegreeOfParallelism = maxDegree;
            PrimeCheck primeCheck = new PrimeCheck();

            secondNum += 1;


            if (primeCheck.isValidNumbers(firstNum, secondNum))
            {
                Parallel.For(firstNum, secondNum, op, numberToCheck =>
                {
                    if (primeCheck.IsPrime(numberToCheck))
                    {
                        resultList.Add(numberToCheck);
                    }
                });
            }
            return(resultList);
        }