Here is a working example for Winform app with a BackgroundWorker and ProgressBar:
1.
Form1.Designer.cs
private void InitializeComponent()
{
this.MyBackgroundWorker = new System.ComponentModel.BackgroundWorker();
this.button1 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
this.MyBackgroundWorker.WorkerReportsProgress = true;
this.MyBackgroundWorker.WorkerSupportsCancellation = true;
this.MyBackgroundWorker.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.MyBackgroundWorker_DoWork);
this.MyBackgroundWorker.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler(
this.MyBackgroundWorker_ProgressChanged);
this.MyBackgroundWorker.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandler(
this.MyBackgroundWorker_RunWorkerCompleted);
this.button1.Location = new System.Drawing.Point(15, 16);
this.button1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(78, 20);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.progressBar1.Location = new System.Drawing.Point(15, 49);
this.progressBar1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(519, 20);
this.progressBar1.TabIndex = 1;
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(560, 274);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button1);
this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
2.
Form1.cs
public partial class Form1 : Form
{
public Form1()
=> InitializeComponent();
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
MyBackgroundWorker.RunWorkerAsync();
}
private void MyBackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
MyBackgroundWorker.ReportProgress(i);
}
}
private void MyBackgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void MyBackgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
MessageBox.Show("Completed!");
}
}
Create a new project, add the code, compile, and run. See how it works.
Now, modify your project using the example above.
Hope this helps!