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控件里*/
}
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;
}