首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

BackgroundWorker如何传递参数

2014-01-08 
BackgroundWorker怎么传递参数private BackgroundWorker searchKey new BackgroundWorker()private voi

BackgroundWorker怎么传递参数

        private BackgroundWorker searchKey = new BackgroundWorker();
        private void Button_Click(object sender, EventArgs e)
        {
            string [] st=new string[2];
            st[0] = this.Item.Text;
            st[1] = this.Info.Text;
            searchKey.RunWorkerAsync(st);
        }
        private void searchKey_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] st = new string[2];
            st[0] = ((string[])(e.Argument))[0];
            st[1] = ((string[])(e.Argument))[1];
            
            /*中间省略N步*/
            ListViewItem lv = new ListViewItem(new string[] { list[1], list[2], list[3], list[4] });
            lv.ImageKey = list[0];
            lv.Tag = list[5];
            this.searchKey.ReportProgress(100, lv);
        }
        private void searchKey_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            /*e.UserState
            this.listView.Items.Add();这里面我想加载到ListView控件里*/
        }


整个过程有两次参数传递,因为都不懂怎么参数传递,以前都是设置公共变量,因为初用这个传递参数,现在参数是传递了,但是不知道要怎么取值,请大家帮帮忙。另外,如果有多个BackgroundWorker同时在运行,这样传递参数与取值会不会有问题?应该要怎么做
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

DoWork是子线程跑的,这可以肯定的,所以只能放到DoWork外面去要移动ProceessChanges里
lv 是由DoWork创建的也不行吗?如果DoWork的工作只是查询数据库,而一大堆的判断都在ProceessChanges里做?

要记住所有的控件最好不要放到线程里去创建,线程里调控件必需用委托实现。DoWork的工作只做与UI介面无关的事,判断可以,只要不操作控件UI就可以。
那我这个ListView分组的应该怎么写?
帮个忙写一下可以吗?


这样该明白了吧


 private void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (this.backgroundWorker.IsBusy != true)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    this.listView.Enabled = false;
                    this.listView.Items.Clear();
                    this.listView.BeginUpdate();
                    this.AddItem.Visible = false;
                    this.progressBar1.Visible = true;
                });
                this.backgroundWorker.RunWorkerAsync();
            }


        }



 private class ResultInfo
        {

            public string _sn;
            public string _item;
            public string _classification;
            public string _description;

            public string _design;
            public string _proofread;
            public string _crafts ;
            public string _audit;
            public string _jde;
            public string _finishTime;
            public string _totalTime;
        }
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            OleDbConnection timeCon = new OleDbConnection(tCon);
            OleDbConnection dataCon = new OleDbConnection(con);
            try
            {
                string tSql = @"select * from [新油缸目录-Time] where";
                string dt = "2012-12-12";
                if (Program.userGroup == "工程部")
                {
                    if (Program.userPost == "部门主管" 
[解决办法]
 Program.userPost == "部门经理")
                        tSql = tSql + " FinishTime =#" + dt + "#";
                    else if (Program.userPost == "部门职员")
                    {
                        tSql = tSql + @" Design ='无'";
                        tSql = tSql + @" or (Design='" + Program.userName + "' and Proofread='无')";
                        tSql = tSql + @" or (Design='" + Program.userName + "' and Proofread<>'无' and JDE='无')";
                        tSql = tSql + @" or (Proofread='" + Program.userName + "' and Crafts ='无')";
                    }
                }
                else if (Program.userGroup == "制造部")
                {
                    if (Program.userPost == "部门主管" 
[解决办法]
 Program.userPost == "部门经理")
                        tSql = tSql + " FinishTime =#" + dt + "#";
                    else
                        tSql = tSql + " Crafts=='" + Program.userName + "' and Audit ='无'";
                }


                else return;
                if (timeCon.State == ConnectionState.Closed) timeCon.Open();
                if (dataCon.State == ConnectionState.Closed) dataCon.Open();
                OleDbCommand timeCmd = new OleDbCommand(tSql, timeCon);
                OleDbDataReader timeRead = timeCmd.ExecuteReader();

                int index = 0;
                while (timeRead.Read())
                {
                    string _sn = timeRead["SN"].ToString();
                    string _item = timeRead["Item"].ToString();
                    string _classification = timeRead["Classification"].ToString();
                    string _description = string.Empty;
                    string sql = @"select SN,Description from [新油缸目录] where SN='" + _sn + "'";
                    OleDbCommand dataCmd = new OleDbCommand(sql, dataCon);
                    OleDbDataReader dataRead = dataCmd.ExecuteReader();
                    if (dataRead.Read())
                    {
                        _description = dataRead["Description"].ToString();
                    }
                    dataRead.Close();
                    dataCmd.Dispose();
                    string _design = timeRead["Design"].ToString();
                    string _proofread = timeRead["Proofread"].ToString();
                    string _crafts = timeRead["Crafts"].ToString();
                    string _audit = timeRead["Audit"].ToString();
                    string _jde = timeRead["JDE"].ToString();
                    string _finishTime = DateTime.Parse(timeRead["FinishTime"].ToString()).ToString("yyyy-MM-dd");
                    string _totalTime = timeRead["TotalTime"].ToString();

                    ResultInfo info = new ResultInfo();
                    info._sn = _sn;
                    info._item = _item;
                    info._classification = _classification;
                    info._description = _description;

                    info._design = _design;
                    info._proofread = _proofread;
                    info._crafts = _crafts;


                    info._audit = _audit;
                    info._jde = _jde;
                    info._finishTime = _finishTime;
                    info._totalTime = _totalTime;
                    index++;
                    this.backgroundWorker.ReportProgress(index, info);
                }
                timeRead.Close();
                timeCmd.Dispose();
            }
            finally
            {
                if (dataCon.State == ConnectionState.Open) dataCon.Close();
                if (timeCon.State == ConnectionState.Open) timeCon.Close();
            }
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

            ResultInfo info = e.UserState as ResultInfo;

            ListViewItem lv = new ListViewItem();

            if (info._design == "无")/*新的Item*/
            {
                lv = new ListViewItem(new string[] { info._sn, info._item, "", info._description }, null, NewItem);
            }
            else if (info._design != "无" && info._proofread == "无")/*已分配设计任务*/
            {
                lv = new ListViewItem(new string[] { info._sn, info._item, info._design, info._description }, null, Design);
            }
            else if (info._jde != "" && info._crafts == "无")/*待输入JDE系统*/
            {
                lv = new ListViewItem(new string[] { info._sn, info._item, info._design, info._description }, null, JDE);
            }
            else if (info._proofread != "无" && info._crafts == "无")/*已分配校对任务*/
            {
                lv = new ListViewItem(new string[] { info._sn, info._item, info._proofread, info._description }, null, Proofread);
            }
            else if (info._crafts != "无" && info._audit == "无")/*已分配工艺检查*/
            {
                lv = new ListViewItem(new string[] { info._sn, info._item, info._crafts, info._description }, null, Crafts);
            }
            else if (info._audit != "无" && info._finishTime == "2012-12-12")/*已分配审核任务*/
            {
                lv = new ListViewItem(new string[] { info._sn, info._item, info._audit, info._description }, null, Audit);


            }

            if (info._classification == "拉杆缸") lv.ForeColor = System.Drawing.Color.Green;
            else if (info._classification == "冶金缸") lv.ForeColor = System.Drawing.Color.OrangeRed;
            else if (info._classification == "方缸") lv.ForeColor = System.Drawing.Color.DeepPink;
            else if (info._classification == "外协缸") lv.ForeColor = System.Drawing.Color.Blue;
            else if (info._classification == "密封包") lv.ForeColor = System.Drawing.Color.SaddleBrown;
            else if (info._classification.IndexOf("cancel") > -1) lv.ForeColor = System.Drawing.Color.Red;


            this.listView.Items.Add(lv);
            this.progressBar1.Value = e.ProgressPercentage;
        }



热点排行