Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Where am I wrong? I don't see the time remaining during progressbar1's process, only at the end.
Start from this button.
C#
private void btnStart_Click(object sender, EventArgs e)
       {
           blackRows();
       }


What I have tried:

public void blackRows()
       {
           try
           {
               DateTime starttime = DateTime.Now;
               TimeSpan timespent = DateTime.Now - starttime;
               int secondsremaining = (int)(timespent.TotalSeconds / progressBar1.Value * (progressBar1.Maximum - progressBar1.Value));
               int row_count = dataGridView1.Rows.Count;
               progressBar1.Maximum = row_count;
               progressBar1.Step = 1;
               CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
               foreach (DataGridViewRow rw in dataGridView1.Rows)
               {
                   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();
                       progressBar1.PerformStep();
                       LblContatore.Text = secondsremaining.ToString();
                   }
               }
           }
           catch (Exception)
           {
               MessageBox.Show("Error");
           }
           MessageBox.Show("Completed", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
       }


inside public void i added
C#
DateTime starttime = DateTime.Now;
              TimeSpan timespent = DateTime.Now - starttime;
              int secondsremaining = (int)(timespent.TotalSeconds / progressBar1.Value * (progressBar1.Maximum - progressBar1.Value))
Posted
Updated 28-Nov-22 19:37pm
Comments
0x01AA 28-Nov-22 13:24pm    
Looks like this is winform? And the GUI thread has no chance to update.
Dirty way 1: Call LblContatore.Refresh(); after setting the text property
Very dirty way 2: Place Application.DoEvents(); in your loop

Keep in mind, both solutions are not clean ;)
PIEBALDconsult 28-Nov-22 13:28pm    
Run the long-running process on a separate thread?
0x01AA 28-Nov-22 13:33pm    
But that does not help per se to have a actual progress visulaization ;)
PIEBALDconsult 28-Nov-22 13:40pm    
It can. The OP may have to work on it a bit though. Probably a re-design is in order -- to eliminate the nasty grid at least.
0x01AA 28-Nov-22 13:47pm    
"to eliminate the nasty grid at least.": Yes, fully agree

The problem is that your code is all executing on the main thread: the UI thread. And that means that Paint messages aren't responded to until your method ends - by which time the progress bar is at the end.

The solution is to move the long running code onto a separate thread (use a BackgroundWorker Class[^] and it has a Progress reporting mechanism you can use to update your ProgressBar.

Give it a try, it's pretty simple to do once you get the hang of it and the link provides example code.
 
Share this answer
 
Comments
Member 15771242 28-Nov-22 18:20pm    
Ok thanks I have BackGroundWorker, I just don't understand where to put the code blackRows();, in DoWork, in ProgressChange or in BackGroundWorker_Completed? Can you give me an example using my blackRows() code?
To add to OriginalGriff's answer, there are a number of examples available to you on how to implement updating controls using a BackgroundWorker class.

Here is a Search with many working solutions: BackgroundWorker datagrid update - Google Search[^]
 
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