Exemplo n.º 1
0
        private void _backgroundButton_Click(object sender, EventArgs e)
        {
            if (this._backgroundButton.Text == "Cancel")
            {
                timer1.Stop();
                _backgroundButton.Text = "Calc";
                _digits.Enabled        = true;
                backgroundWorker1.CancelAsync();
                return;
            }
            else

            {
                timer1.Start();


                pi = new PiObject
                {
                    calcValue = "",
                    digits    = (int)_digits.Value,
                };

                _digits.Enabled = false;

                _backgroundButton.Text = "Cancel";
                _pi.Text = string.Empty;


                backgroundWorker1.RunWorkerAsync(pi);
            }
        }
Exemplo n.º 2
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            PiObject piObj = e.Argument as PiObject;

            backgroundWorker1 = sender as BackgroundWorker;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.WorkerReportsProgress      = true;

            StringBuilder pi    = new StringBuilder("3", piObj.digits + 2);
            var           watch = System.Diagnostics.Stopwatch.StartNew();

            if (piObj.digits > 0)
            {
                pi.Append(".");

                for (int i = 0; i < piObj.digits; i += 9)
                {
                    if (!this.backgroundWorker1.CancellationPending)
                    {
                        int    nineDigits = NineDigitsofPi.StartingAt(i + 1);
                        int    digitCount = Math.Min(piObj.digits - i, 9);
                        String ds         = String.Format("{0:D9}", nineDigits);
                        pi.Append(ds.Substring(0, digitCount));
                        piObj.calcValue = pi.ToString();
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }// the code that you want to measure comes here
                watch.Stop();
            }
            var elapsedMs = watch.ElapsedMilliseconds;

            piObj.elapsedMs = elapsedMs.ToString() + " ms";
        }