private void ShowProgress(int i) { if (lblOutput.InvokeRequired) { // This is hit if a background thread calls ShowProgress() var showProgressDel = new ShowProcessDelegate(ShowProgress); this.BeginInvoke(showProgressDel, new object[] { i }); } else { lblOutput.Text = i.ToString(); pbStatus.Value = i; } }
private void ShowProgress(int i) { // this is hit if a background thread calls ShowProgress if (lblOutput.InvokeRequired) { // trick here is to assign the ShowProcess method to ShowProcessDelegate var del = new ShowProcessDelegate(ShowProgress); this.BeginInvoke(del, new object[] { i }); } else { lblOutput.Text = i.ToString(); pbStatus.Value = i; } }
//update of controls will takes place here private void ShowProgress(int i) { //1- we need to check if a label needs to be invoked in the GUI thread // - you can do that using "InvokeRequired" property if (lblOutput.InvokeRequired == true) { // 2- if true this is hit by a background thread, so route the data from the background thread to the main thread // we need to use another delegate and pass the data(i) to the GUI Thread. // so we need to integrate another delegate with another async invocation, but this particular delegate // will be involved of routing the data from the background thread to main thread. // recursive call var del = new ShowProcessDelegate(ShowProgress); // using the word "this" which represent the "form" (main thread) with "BeginInvoke" will make // the main thread to run, so we re call the function and "else" will be hit, because "InvokeRequired" will be false this.BeginInvoke(del, new Object[] { i }); } else { lblOutput.Text = i.ToString(); pbStatus.Value = i; } }
void ShowProgress(string pi, int totalDigits, int digitsSoFar, out bool cancel) { if (_pi.InvokeRequired == false) // UI thread running { cancel = _calcState == CalcState.Canceled; if (cancel) { _calcState = CalcState.Pending; _calcButton.Enabled = true; _calcButton.Text = "Calc"; } _pi.Text = pi; _piProgress.Maximum = totalDigits; _piProgress.Value = digitsSoFar; } else { ShowProcessDelegate del = new ShowProcessDelegate(ShowProgress); object[] args = { pi, totalDigits, digitsSoFar, null }; this.Invoke(del, args); cancel = (bool)args[3]; } }