Exemple #1
0
        // <snippet40>
        // This event handler updates the ListView control when the
        // PrimeNumberCalculator raises the ProgressChanged event.
        //
        // On fast computers, the PrimeNumberCalculator can raise many
        // successive ProgressChanged events, so the user interface
        // may be flooded with messages. To prevent the user interface
        // from hanging, progress is only reported at intervals.
        private void primeNumberCalculator1_ProgressChanged(
            ProgressChangedEventArgs e)
        {
            if (this.progressCounter++ % this.progressInterval == 0)
            {
                Guid taskId = (Guid)e.UserState;

                if (e is CalculatePrimeProgressChangedEventArgs)
                {
                    CalculatePrimeProgressChangedEventArgs cppcea =
                        e as CalculatePrimeProgressChangedEventArgs;

                    this.UpdateListViewItem(
                        taskId,
                        cppcea.ProgressPercentage,
                        cppcea.LatestPrimeNumber);
                }
                else
                {
                    this.UpdateListViewItem(
                        taskId,
                        e.ProgressPercentage);
                }
            }
            else if (this.progressCounter > this.progressInterval)
            {
                this.progressCounter = 0;
            }
        }
Exemple #2
0
        private void CalculateWorker(int numberToTest, AsyncOperation asyncOp)
        {
            bool      isPrime      = false;
            int       firstDivisor = 1;
            Exception e            = null;

            if (!TaskCanceled(asyncOp.UserSuppliedState))
            {
                try
                {
                    int n = 0;
                    ProgressChangedEventArgs eventArgs = null;
                    while (n < int.MaxValue && !TaskCanceled(asyncOp.UserSuppliedState))
                    {
                        float percentage = (float)n * 100 / int.MaxValue;
                        if ((percentage % 1) > 0.000001)
                        {
                            eventArgs = new CalculatePrimeProgressChangedEventArgs(n, (int)((float)n / (float)numberToTest * 100), asyncOp.UserSuppliedState);
                            asyncOp.Post(this.onProgressReportDelegate, eventArgs);
                            Thread.Sleep(0);
                        }
                        n++;
                    }
                }
                catch (Exception ex)
                {
                    e = ex;
                }
            }

            this.CompletionMethod(numberToTest, firstDivisor, isPrime, e, TaskCanceled(asyncOp.UserSuppliedState), asyncOp);
        }
Exemple #3
0
        private void primeNumberCalculator1_ProgressChanged(ProgressChangedEventArgs e)
        {
            if (this.progressCounter++ % this.progressInterval == 0)
            {
                Guid taskId = (Guid)e.UserState;

                if (e is CalculatePrimeProgressChangedEventArgs)
                {
                    CalculatePrimeProgressChangedEventArgs cppcea = e as CalculatePrimeProgressChangedEventArgs;
                }
                else
                {
                }
            }
            else if (this.progressCounter > this.progressInterval)
            {
                this.progressCounter = 0;
            }
        }
Exemple #4
0
        // This method computes the list of prime numbers used by the
        // IsPrime method.
        private ArrayList BuildPrimeNumberList(
            int numberToTest,
            AsyncOperation asyncOp)
        {
            ProgressChangedEventArgs e = null;
            ArrayList primes           = new ArrayList();
            int       firstDivisor;
            int       n = 5;

            // Add the first prime numbers.
            primes.Add(2);
            primes.Add(3);

            // Do the work.
            while (n < numberToTest &&
                   !TaskCanceled(asyncOp.UserSuppliedState))
            {
                if (IsPrime(primes, n, out firstDivisor))
                {
                    // Report to the client that a prime was found.
                    e = new CalculatePrimeProgressChangedEventArgs(
                        n,
                        (int)((float)n / (float)numberToTest * 100),
                        asyncOp.UserSuppliedState);

                    asyncOp.Post(this.onProgressReportDelegate, e);

                    primes.Add(n);

                    // Yield the rest of this time slice.
                    Thread.Sleep(0);
                }

                // Skip even numbers.
                n += 2;
            }

            return(primes);
        }