private void StopBtn_Click(object sender, EventArgs e) { //RunCalcTimer.Enabled = StopBtn.Enabled = false; SimBackgroundWorker.CancelAsync(); StartBtn.Enabled = RunForeverCheckbox.Enabled = IterationsToRunTextBox.Enabled = IterationsToRunLabel.Enabled = true; }
private void SimBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { while (!SimBackgroundWorker.CancellationPending) { if (_iterationsRan <= _iterationsToRun || RunForeverCheckbox.Checked) { //Console.WriteLine(iterationsRan); _iterationsRan++; var x = _random.NextDouble() * 2 - 1; var y = _random.NextDouble() * 2 - 1; if (x * x + y * y < 1.0) { var xToDraw = x * Scaling + 250; var yToDraw = y * Scaling + 250; _insideUnitCircle++; try { _graphics.DrawRectangle(_bluePx, (int)xToDraw, (int)yToDraw, 1, 1); } catch (Exception exception) { Console.WriteLine(exception); } } else { var xToDraw = x * Scaling + 250; var yToDraw = y * Scaling + 250; try { _graphics.DrawRectangle(_redPx, (int)xToDraw, (int)yToDraw, 1, 1); } catch (Exception exception) { Console.WriteLine(exception); } } } else if (_iterationsRan == _iterationsToRun) { SimBackgroundWorker.ReportProgress(1); SimBackgroundWorker.CancelAsync(); break; } SimBackgroundWorker.ReportProgress(1); } }
private void ResetButton_Click(object sender, EventArgs e) { //wipe panel of darts DrawPanel.Invalidate(); ApproxPiNum.Text = SimsLabelNum.Text = PercentDifferenceLabelNum.Text = "0"; SimBackgroundWorker.CancelAsync(); StartBtn.Enabled = true; StopBtn.Enabled = false; RunForeverCheckbox.Enabled = true; IterationsToRunTextBox.Enabled = true; IterationsToRunLabel.Enabled = true; //resets vars as to not effect future iterations _percentDifference = 0; _approximatedPi = 0; _insideUnitCircle = 0; _iterationsRan = 0; InsideCircleLabel.Text = OutsideCircleLabel.Text = "0"; }