Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if I insert the code in the start button, it works but it doesn't work with dowork but separately, I would like it to be activated in dowork, how to do it?


My cosde:

What I have tried:

<
public void righeNere()
       {
           try
           {
               int row_count = dataGridView1.Rows.Count;
               micheleProgressBar1.Maximum = row_count;
               micheleProgressBar1.Step = 1;
               CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
               foreach (DataGridViewRow rw in dataGridView1.Rows)
               {
                   if (rw.Cells[2].Style.BackColor == Color.Gold && rw.Cells[3].Style.BackColor == Color.Gold && rw.Cells[4].Style.BackColor == Color.Gold &&
                       rw.Cells[5].Style.BackColor == Color.Gold && rw.Cells[6].Style.BackColor == Color.Gold && rw.Cells[7].Style.BackColor == Color.Gold)
                   {
                       rw.Visible = true;
                       continue;
                   }
                   else if (rw.Cells[2].Style.BackColor == Color.White && rw.Cells[3].Style.BackColor == Color.White && rw.Cells[4].Style.BackColor == Color.White
                       && rw.Cells[5].Style.BackColor == Color.White && rw.Cells[6].Style.BackColor == Color.White && rw.Cells[7].Style.BackColor == Color.White)
                   {
                       manager.SuspendBinding();
                       rw.Visible = false;
                       manager.ResumeBinding();
                       micheleProgressBar1.PerformStep();
                   }
               }
           }
           catch (Exception)
           {
           }
       }
Posted
Updated 17-Nov-22 3:51am

Here is a working example for Winform app with a BackgroundWorker and ProgressBar:

1. Form1.Designer.cs
C#
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();
	// 
	// MyBackgroundWorker
	// 
	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);
	// 
	// button1
	// 
	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);
	// 
	// progressBar1
	// 
	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;
	// 
	// Form1
	// 
	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
C#
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!
 
Share this answer
 
Comments
Michele Signorile 17-Nov-22 12:30pm    
Thanks for the answer, ok but, if I wanted to link my code to the progressbar (">this public void righeNere() how to do it?
that would be the code I posted in the question.
Graeme_Grant 17-Nov-22 17:07pm    
MyBackgroundWorker_DoWork method is where the work is done. I show you in that method how to update the progressBar:
MyBackgroundWorker.ReportProgress(i);

You need to wire up the event handlers for it to work. I show you how in the first part:
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);
Michele Signorile 18-Nov-22 7:25am    
Solved, thanks, everything works perfectly.
Graeme_Grant 18-Nov-22 7:32am    
You are most welcome
This CodeProject tip might help: BackgroundWorker and ProgressBar demo[^]
 
Share this answer
 
You can't. DoWork executes code on a background thread, so it won't be able to access any UI elements. Your code is almost exclusively accessing UI elements, so it won't work from a background thread.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900