Click here to Skip to main content
16,019,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
c# I need the timer to run while the progressbar is doing its job with some public code, but I'd like to start them together, how do I do that?
the problem is that if I connect them it works but it does one line at a time and sends the completed message to each line. I would like that while the progress bar does its job, time runs second by second until the progressbar operation is finished.
This is the code that progress bar works:

C#
public void righeNere()
      {
          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.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();
                  MessageBox.Show("Completato", "Messaggio", MessageBoxButtons.OK, MessageBoxIcon.Information);
              }
          }
      }

This is the code that timer2 works:
C#
private void BtnAvvia_Click(object sender, EventArgs e)
        {
            BtnAvvia.Enabled = false;
            BtnFerma.Enabled = true;
            BtnResetta.Enabled = true;
            timer2.Interval = (1000) * (1);
            timer2.Enabled = true;
            timer2.Start();
            startTime = DateTime.Now;
        }
        private void BtnFerma_Click(object sender, EventArgs e)
        {
            BtnFerma.Enabled = false;
            BtnAvvia.Enabled = true;
            BtnResetta.Enabled = true;
            timer2.Stop();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            elapsedTime = DateTime.Now - startTime;
            lblContatore.Text = elapsedTime.ToString(@"hh\:mm\:ss\.f");
        }


What I have tried:

C#
private void BtnAvvia_Click(object sender, EventArgs e)
       {
           BtnAvvia.Enabled = false;
           BtnFerma.Enabled = true;
           BtnResetta.Enabled = true;
           timer2.Interval = (1000) * (1);
           timer2.Enabled = true;
           timer2.Start();
           startTime = DateTime.Now;
           righeNere();
       }
       private void BtnFerma_Click(object sender, EventArgs e)
       {
           BtnFerma.Enabled = false;
           BtnAvvia.Enabled = true;
           BtnResetta.Enabled = true;
           timer2.Stop();
       }
       private void timer2_Tick(object sender, EventArgs e)
       {
           elapsedTime = DateTime.Now - startTime;
           lblContatore.Text = elapsedTime.ToString(@"hh\:mm\:ss\.f");
       }
       public void righeNere()
       {
           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.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();
                   MessageBox.Show("Completato", "Messaggio", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
           }
       }
Posted
Updated 23-Nov-22 1:30am
Comments
[no name] 22-Nov-22 13:00pm    
Timer tick updates the progress bar's .Value; .MaxValue should reflect the estimated total run time; if that's what you're asking.
Member 15771242 22-Nov-22 13:12pm    
Thanks for the reply, but I don't know how or how much value I have to give the timer, can you give me an example?

Your issue is that you are doing all the work on the UI thread, and not a background thread. You should be using a BackgroundWorker on the data and not work directly on the DataGridView.

With a quick Google Search: datagridview and backgroundworker example - Google Search[^]

I found this example: Using BackgroundWorker in VB.Net to update a datagrid[^]. It is in VB, but it is easy to read, shows you what you need to know, and should not be too difficult to convert to C#.

Here is another example from the above Google Search: DataGrid using BackgroundWorker - C#[^] - same concept, just a little bit more involved example.
 
Share this answer
 
v2
[SOLVED] thanks with backgroundworker everything works.
 
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