首页 新闻 会员 周边 捐助

BackgroundWorker为什么程序点击按钮时,一发布就死机,本地OK。

0
悬赏园豆:15 [待解决问题]

一:DO_Work代码

   private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            BuildCode(worker, e);
        }

二:BuildCode

private void BuildCode(BackgroundWorker worker, DoWorkEventArgs e)
        {
            try
            {
                bool hasChecked = false;
                if (this.lv_tbview.Items.Count > 0)
                {
                    for (int i = 0; i < lv_tbview.Items.Count; i++)
                    {
                        if (lv_tbview.Items[i].Checked == true)
                        {
                            hasChecked = true;
                        }
                    }
                    if (hasChecked == false)
                    {
                        throw new Exception("你还没有选择任何数据库表字段!");
                        return;
                        //MessageBox.Show("你还没有选择任何数据库表字段!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //return;
                    }

                    this.tabControl1.SelectedIndex = 1;

                    for (int i = 0; i < 10; i++)
                    {
                        if (worker.CancellationPending == true)
                        {
                            e.Cancel = true;
                            break;
                        }
                        else
                        {
                            Thread.Sleep(500);
                            worker.ReportProgress(i * 10);
                        }
                    }是这边问题么?
                    string tbName = this.labelTbName.Text.Exp_Trim();
                    DataTable dt = DalUtility.GetTableInfo(tbName, this.login);
                    if (radioButtonM.Checked)
                    {
                        builder = YixianCoderFactory.CreateCoderFactory("M");
                        this.rt_code.Text = builder.BuildCode(dt, tbName);
                    }
                    if (radioButtonD.Checked)
                    {
                        builder = YixianCoderFactory.CreateCoderFactory("D");
                        this.rt_code.Text = builder.BuildCode(dt, tbName);
                    }
                    if (radioButtonB.Checked)
                    {
                        builder = YixianCoderFactory.CreateCoderFactory("B");
                        this.rt_code.Text = builder.BuildCode(dt, tbName);
                    }执行代码
                }
                else
                {
                    throw new Exception("你还没有选择数据库表!");
                    return;
                    //MessageBox.Show("你还没有选择数据库表!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                //记录日志
                throw ex;
                //MessageBox.Show("生成出错,详细信息:" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

三:ProgressChanged

  private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.resultLabel.Text = temp.Exp_Format("进度报告:" + e.ProgressPercentage.ToString() + "%");
        }

四:RunWorkerCompleted

private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                resultLabel.Text = temp.Exp_Format("错误源:" + e.Error.Message);
                return;
            }
            else if (e.Cancelled == true)
            {
                resultLabel.Text = temp.Exp_Format("已取消!");
                return;
            }
            else
                resultLabel.Text = temp.Exp_Format("生成完毕!");

            this.buttonBuilder.Enabled = true;
            this.buttonCancel.Enabled = false;
        }

范立勇的主页 范立勇 | 初学一级 | 园豆:20
提问于:2013-10-22 13:29
< >
分享
所有回答(1)
1

看了百度百科的一段介绍:

您必须非常小心,确保在 DoWork 事件处理程序中不操作任何用户界面对象。而应该通过 ProgressChanged和 RunWorkerCompleted 事件与用户界面进行通信。

后来代码修改如下:

一:搞个 用于 backgroundWorker.RunWorkerAsync(new BuildParamsInput(builder, dt, tbName));参数传递的对象

public struct BuildParamsInput
        {
            public IBuilder builder;
            public DataTable dt;
            public string tabName;

            public BuildParamsInput(IBuilder builder, DataTable dt, string tabName)
            {
                this.builder = builder;
                this.dt = dt;
                this.tabName = tabName;
            }
        }

二:在DoWork中如下,注意一定不可以与用户界面的任何控件沾边(否则就发生如上面的问题,界面假死)

 private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            CreateCode(worker, e);

        }

三: 也贴上CreateCode的代码

private void CreateCode(BackgroundWorker worker, DoWorkEventArgs e)
        {
            BuildParamsInput buildParamsInput = (BuildParamsInput)e.Argument;
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(500);
                worker.ReportProgress(i * 10);
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

            }
            //this.rt_code.Text = builder.BuildCode(dt, tbName);
            e.Result = buildParamsInput.builder.BuildCode(buildParamsInput.dt, buildParamsInput.tabName);
        }

范立勇 | 园豆:20 (初学一级) | 2013-10-24 10:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册