使用backgroudWorker C#时发生跨线程操作错误

时间:2018-05-19 02:56:23

标签: c# backgroundworker cross-thread

我想在使用backgroundWorker将数据提取到文本框时显示进度条。但是在调试时,它引发了以下错误:

我看过类似的情况,但无法解决我的问题。

  

System.InvalidOperationException:跨线程操作无效   使用Texbox txtResults。

我是C#的新手,请帮助我!谢谢!

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;
        }
    }

backgroundWorker DoWork时发生错误。

已编辑:已添加

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

0 个答案:

没有答案