想多次运用同一BackgroundWorker对象进行线程操作
private void btnSingleCopy_Click(object sender, EventArgs e)
{
Done = 0;
tableName[0] = comboBox3.Text;
tableName[1] = comboBox4.Text;
Currentbgw.WorkerReportsProgress = true;//报告完成进度
Currentbgw.WorkerSupportsCancellation = true;//允许用户终止后台线程
Currentbgw.DoWork += new DoWorkEventHandler(SingleDoWork);
Currentbgw.ProgressChanged += new ProgressChangedEventHandler(SingleUpdateProgress);
Currentbgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SingleCompletedWork);
if (!Currentbgw.IsBusy)//判断backgroundWorker1是否正在运行异步操作
{
Currentbgw.RunWorkerAsync(tableName);//开始执行后台操作
}
}
void SingleCompletedWork(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("处理完成!");
Done = 0;
progressBar1.Value = 0;
Currentbgw.Dispose();
}
当第二次btnSingleCopy_Click事件后,e.ProgressPercentage传递给progressBar1.Value 的值从101开始,而不是从0开始的,导致progressBar1.Value 越界,怎么解决这个问题啊?
void SingleUpdateProgress(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;//将完成进度数据传给进度条
label11.Text = Convert.ToInt32(progressBar1.Value).ToString() + "%";
}
解决了吗?
过去时间比较长了,但是已解决
@一碗周: 请问你有用过线程同步吗?
@码农改变世界lyf: 直接用的backgroundworker做的,不过过去时间挺长了,有点忘了