void CalcPi(int digits) { StringBuilder pi = new StringBuilder("3", digits + 2); object sender = System.Threading.Thread.CurrentThread; ShowProgressArgs e = new ShowProgressArgs(pi.ToString(), digits, 0); // Show progress ShowProgress(sender, e); if (digits > 0) { pi.Append("."); for (int i = 0; i < digits; i += 9) { int nineDigits = NineDigitsOfPi.StartingAt(i + 1); int digitCount = Math.Min(digits - i, 9); string ds = string.Format("{0:D9}", nineDigits); pi.Append(ds.Substring(0, digitCount)); // Show progress e.Pi = pi.ToString(); e.DigitsSoFar = i + digitCount; ShowProgress(sender, e); if (e.Cancel) { break; } } } }
void ShowProgress(object sender, ShowProgressArgs e) { // Make sure we're on the UI thread if (_pi.InvokeRequired == false) { _pi.Text = e.Pi; _piProgress.Maximum = e.TotalDigits; _piProgress.Value = e.DigitsSoFar; // Check for Cancel e.Cancel = (_state == CalcState.Canceled); // Check for completion if (e.Cancel || (e.DigitsSoFar == e.TotalDigits)) { _state = CalcState.Pending; _calcButton.Text = "Calc"; _calcButton.Enabled = true; } } else { // Get ready to show progress asynchronously ShowProgressHandler showProgress = new ShowProgressHandler(ShowProgress); // Show progress Invoke(showProgress, new object[] { sender, e }); } }
static void lz78_showProgres(object sender, ShowProgressArgs e) { Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 2); Console.WriteLine(); int x = (int)(((double)e.current_size / (double)e.full_size) * 100); Console.WriteLine(x + "% complete"); }
protected virtual void OnShowProgress(ShowProgressArgs e) { showProgress?.Invoke(this, e); }