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:
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:
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:
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);
}
}
}