Cross-thread operation error occur when using backgroudWorker C#

63 Views Asked by At

I want to display a progress bar when fetching data to textbox using backgroundWorker. But when debugging, it raised an the following error:

I have read the similar case but It cannot solve my problem.

System.InvalidOperationException: Cross-thread operation not valid with Texbox txtResults.

I am new to C# , please help me! Thanks!

private void btnCheckProcStep_Click(object sender, EventArgs e)
        {
            lblStatus.Text = "";
            if (backgroundWorker1.IsBusy)
            {
                backgroundWorker1.CancelAsync();
            }
            else
            {
                progressBar1.Visible = true;
                progressBar1.Value = progressBar1.Minimum;

                backgroundWorker1.RunWorkerAsync();
            }
        }
        private void SetText(string text)
        {
            if (this.txtResults.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.txtResults.Text = text;
            }
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                txtResults.Clear();
                DataTable dtx = new DataTable();
                foreach (DataGridViewRow row in grdMametanCheckList.Rows)
                {
                    var _MAMETAN_NO = row.Cells[0].Value.ToString();
                    dtx = get_Missing_Proc_Inst_Seq(_MAMETAN_NO);

                    foreach (DataRow dr in dtx.Rows)
                    {
                        txtResults.Text += row.Cells[0].Value.ToString() + "," + dr[0].ToString() + Environment.NewLine;
                    }
                }
                for (int i = 0; i <= 100; i++)
                {
                    SetText(i.ToString() + " %");
                    backgroundWorker1.ReportProgress(i);
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }            
        }



private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        if (!backgroundWorker1.CancellationPending)
        {
            lblStatus.Text = "Processing ... " + e.ProgressPercentage + "%";
            progressBar1.Value = e.ProgressPercentage;
        }
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            lblStatus.Text = e.Error.Message;
        }
        else
        {
            lblStatus.Text = "Done";
            progressBar1.Value = 0;
            progressBar1.Visible = false;
        }
    }

The error occur when backgroundWorker DoWork.

EDITED: added

              foreach (DataRow dr in dtx.Rows)
                    {
                        txtResults.Text += row.Cells[0].Value.ToString() + "," + dr[0].ToString() + Environment.NewLine;
                        SetText(txtResults.Text.ToString());
                    }
0

There are 0 best solutions below