Hello,
I want to change a ProgressBar in a separate Thread.
My current code is the following:
private void changeProgressB arThread()
{
changeProgressB ar();
}
private void changeProgressB ar()
{
toolStripProgre ssBar1.Maximum = 100;
for (int i = 0; i <= 100; i++)
{
toolStripProgre ssBar1.Value = i;
if (i == 100)
{
for (int j = 100; j >= 0; j++)
{
toolStripProgre ssBar1.Value = j;
}
i = 0;
}
}
}
private void button1_Click(o bject sender, EventArgs e)
{
ThreadStart thrdDeleg1 = new ThreadStart(add DataSetThread);
Thread addDSThrd1 = new Thread(thrdDele g1);
ThreadStart thrdDeleg2 = new ThreadStart(cha ngeProgressBarT hread);
Thread addDSThrd2 = new Thread(thrdDele g2);
addDSThrd1.Star t();
addDSThrd2.Star t();
while (addDSThrd1.IsA live)
{
if(!addDSThrd1. IsAlive)
addDSThrd2.Abor t();
}
addDSThrd1.Join ();
string[] row = new String[3];
row[0] = searchText_;
row[1] = results_.count. ToString();
row[2] = "0";
dgViewData.Rows .Add(row);
}
My problem is now, that I cannot use the toolStripProgre ssBar1 object:
"Cross-thread operation not valid: Control '' accessed from a thread
other than the thread it was created on."
I understand the error message but... How can I fix the error?
I do not know how to update my progress bar if I cannot update it in my
seperate thread.
How do you do that?
It would be nice if you could give me a small example.
And my other question concerns the following code lines:
while (addDSThrd1.IsA live)
{
if(!addDSThrd1. IsAlive)
addDSThrd2.Abor t();
}
I think these lines of code are not very well/ secure,
because it looks bit like an endless loop.
How do you let another thread (here my progress bar thread) stop if a
long calculation in another thread is finished?
Regards,
Martin