// 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." }); }
// 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." }); }