Example #1
0
        // prime thread method
        private void calculatePrimes()
        {
            PerformStepDelegate performStep = new PerformStepDelegate(progressBar.PerformStep);
            //MethodInvoker performStep = delegate { progressBar.PerformStep(); };
            AppendTextDelegate    appendText            = new AppendTextDelegate(txtBoxResults.AppendText);
            SetTxtBoxProgressText setTxtBoxProgressText = delegate(string s) { txtBoxProgress.Text = s; };

            uint count     = 0;
            uint stepValue = numberOfPrimes / 100;

            if (stepValue == 0)
            {
                stepValue = 1;
            }

            this.Invoke(appendText, new object[] { 2 + "\r\n" });
            if (++count == numberOfPrimes)
            {
                goto end;
            }

            for (ulong n = 3; ; n += 2)
            {
                if (isPrime(n))
                {
                    this.Invoke(appendText, new object[] { n + "\r\n" });
                    if (++count == numberOfPrimes)
                    {
                        break;
                    }
                    if (count % stepValue == 0)
                    {
                        this.Invoke(performStep);
                        //this.Invoke(setTxtBoxProgressText, new object[] { progressBar.Value + "%" });
                        this.Invoke((MethodInvoker) delegate { txtBoxProgress.Text = progressBar.Value + "%"; });
                    }
                }
            }

end:
            this.Invoke((MethodInvoker) delegate { progressBar.Value = 100; });
            this.Invoke((MethodInvoker) delegate { txtBoxProgress.Text = "100%"; });
            this.Invoke((MethodInvoker) delegate { btnGo.Enabled = true; });
            this.Invoke((MethodInvoker) delegate { btnStop.Enabled = false; });
            this.Invoke((MethodInvoker) delegate { btnCancel.Enabled = false; });
            this.Invoke((MethodInvoker) delegate { txtBoxInput.ReadOnly = false; });

            stopWatch.Stop();

            if (checkBoxSave.Checked)
            {
                File.WriteAllText(txtBoxSave.Text, txtBoxResults.Text, Encoding.Unicode);
            }

            CustomTimeSpan t = new CustomTimeSpan(stopWatch.ElapsedMilliseconds);

            this.Invoke(appendText, new object[] { "\r\nCompleted in " + t });
            //this.Invoke(appendText, new object[] { "\r\nCompleted in " + (stopWatch.ElapsedMilliseconds / 1000.0) + " seconds." });
        }
Example #2
0
        // prime thread method
        private void CalculatePrimes()
        {
            // make delegate instances
            //var performStep = new PerformStepDelegate(progressBar.PerformStep);
            MethodInvoker performStep = () => progressBar.PerformStep();

            /*Delegate appendText = (o) =>
             * {
             *  txtBoxResults.AppendText(o.ToString());
             *  txtBoxResults.AppendText("\r\n");
             * };*/
            var appendText = new AppendTextDelegate((o) =>
            {
                txtBoxResults.AppendText(o.ToString());
                txtBoxResults.AppendText("\r\n");
            });
            var setTxtBoxProgressText = new SetTxtBoxProgressText((o) => txtBoxProgress.Text = o + @"%");

            uint count     = 0;
            var  stepValue = _numberOfPrimes / 100;

            if (stepValue == 0)
            {
                stepValue = 1;
            }

            // reset the prime generator
            _primeGenerator.Reset();
            _primeGenerator.SetPrimeListCapacity((int)_numberOfPrimes);

            if (checkBoxShowAll.Checked)
            {
                // add first prime since there will always be at least one
                Invoke(appendText, _primeGenerator.CurrentPrime);

                // add more primes until count reaches numberOfPrimes
                while (++count != _numberOfPrimes)
                {
                    // perform step on progress bar
                    if (count % stepValue == 0)
                    {
                        Invoke(performStep);
                        //this.Invoke((MethodInvoker)delegate { txtBoxProgress.Text = progressBar.Value + @"%"; });
                        Invoke(setTxtBoxProgressText, progressBar.Value);
                    }

                    Invoke(appendText, _primeGenerator.NextPrime());
                }
            }
            else
            {
                while (++count != _numberOfPrimes)
                {
                    // perform step on progress bar
                    if (count % stepValue == 0)
                    {
                        Invoke(performStep);
                        //this.Invoke((MethodInvoker)delegate { txtBoxProgress.Text = progressBar.Value + @"%"; });
                        Invoke(setTxtBoxProgressText, progressBar.Value);
                    }

                    _primeGenerator.NextPrime();
                }

                Invoke(appendText, _primeGenerator.CurrentPrime);
            }


            //this.Invoke((MethodInvoker)delegate { progressBar.Value = 100; });
            Invoke((MethodInvoker)(() =>
            {
                progressBar.Value = 100;
                txtBoxProgress.Text = @"100%";
                btnGo.Enabled = checkBoxSave.Enabled = checkBoxShowAll.Enabled = true;
                btnStop.Enabled = btnCancel.Enabled = txtBoxInput.ReadOnly = false;
            }));

            _stopWatch.Stop();

            if (checkBoxSave.Checked)
            {
                File.WriteAllText(txtBoxSave.Text, txtBoxResults.Text, Encoding.Unicode);
            }

            var t = new CustomTimeSpan(_stopWatch.ElapsedMilliseconds);

            Invoke(appendText, "\r\nCompleted in " + t);
        }