我创建了一个带有进度条的小项目来测试BackgroundWorker
如何完成其工作。所以进度条从0到100%。但现在我想做的是在第一个完成工作后触发另一个BackgroundWorker
。第二个BackgroundWorker
只需要显示一个MessageBox。我已将第二个工作人员的RunWorkerAsync()
事件放在第一个工作人员的RunWorkerCompleted()
事件结束时。确保第一个工人的方法完成。
发生的奇怪事情是我的第二个工人的DoWork()
事件被触发两次。
这是我在主程序中遇到的错误的简化版本。
有谁能发现这个问题?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace backgroundworker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.ProgressChanged += new ProgressChangedEventHandler(bw1_ProgressChanged);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bw1.WorkerReportsProgress = true;
bw1.WorkerSupportsCancellation = true;
bw2.DoWork += new DoWorkEventHandler(bw2_DoWork);
bw2.ProgressChanged += new ProgressChangedEventHandler(bw2_ProgressChanged);
bw2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw2_RunWorkerCompleted);
}
private void bw1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(50);
bw1.ReportProgress(i);
if (bw1.CancellationPending)
{
e.Cancel = true;
bw1.ReportProgress(0);
return;
}
}
bw1.ReportProgress(100);
}
private void bw1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
private void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
lblStatus.Text = "Task Cancelled.";
}
else if (e.Error != null)
{
lblStatus.Text = "Error while performing background operation.";
}
else
{
lblStatus.Text = "Task Completed...";
}
btnStartAsyncOperation.Enabled = true;
btnCancel.Enabled = false;
bw2.RunWorkerAsync();
}
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
btnStartAsyncOperation.Enabled = false;
btnCancel.Enabled = true;
bw1.RunWorkerAsync();
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (bw1.IsBusy)
{
bw1.CancelAsync();
}
}
private void bw2_DoWork(object sender, DoWorkEventArgs e)
{
}
private void bw2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void bw2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Second task is done too.");
}
}
}
更新: Form1.Designer.cs
namespace backgroundworker
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.lblStatus = new System.Windows.Forms.Label();
this.bw1 = new System.ComponentModel.BackgroundWorker();
this.btnStartAsyncOperation = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.bw2 = new System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(12, 154);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(1164, 100);
this.progressBar1.TabIndex = 1;
//
// lblStatus
//
this.lblStatus.AutoSize = true;
this.lblStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblStatus.Location = new System.Drawing.Point(469, 42);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(70, 25);
this.lblStatus.TabIndex = 2;
this.lblStatus.Text = "label1";
//
// bw1
//
this.bw1.WorkerReportsProgress = true;
this.bw1.WorkerSupportsCancellation = true;
this.bw1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bw1_DoWork);
this.bw1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bw1_ProgressChanged);
this.bw1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bw1_RunWorkerCompleted);
//
// btnStartAsyncOperation
//
this.btnStartAsyncOperation.Location = new System.Drawing.Point(12, 109);
this.btnStartAsyncOperation.Name = "btnStartAsyncOperation";
this.btnStartAsyncOperation.Size = new System.Drawing.Size(160, 39);
this.btnStartAsyncOperation.TabIndex = 3;
this.btnStartAsyncOperation.Text = "Start";
this.btnStartAsyncOperation.UseVisualStyleBackColor = true;
this.btnStartAsyncOperation.Click += new System.EventHandler(this.btnStartAsyncOperation_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(1024, 109);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(152, 39);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// bw2
//
this.bw2.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bw2_DoWork);
this.bw2.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bw2_ProgressChanged);
this.bw2.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bw2_RunWorkerCompleted);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1188, 272);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnStartAsyncOperation);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.progressBar1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label lblStatus;
private System.ComponentModel.BackgroundWorker bw1;
private System.Windows.Forms.Button btnStartAsyncOperation;
private System.Windows.Forms.Button btnCancel;
private System.ComponentModel.BackgroundWorker bw2;
}
}
答案 0 :(得分:3)
您已在设计师处注册了已完成的活动。
this.bw1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bw1_RunWorkerCompleted);
this.bw2.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bw2_RunWorkerCompleted);
然后你又在代码背后注册了它。
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bw2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw2_RunWorkerCompleted);
您需要删除其中一些。