I simply want two progress bar to increment simultaneously on a button click; i have created two progress bars and one button start
with two threads t1 and t2 each of which contains different method in order to increment the values of two progress bars (t1 for progressbar1 and t2 for progressbar2 respectively).
Controls I have: Progress Bars: progressbar1, progressbar2 and Button button1.
The code is as follows:
Thread t1, t2;
private void Progressor1()
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
for (int i = 0; i <= 100; i++)
progressBar1.Value = i;
}
private void Progressor2()
{
progressBar2.Minimum = 0;
progressBar2.Maximum = 100;
for (int j = 0; j <= 100; j++)
progressBar2.Value = j;
}
private void button1_Click(object sender, EventArgs e) // Start Button
{
t1.Start();
t2.Start();
}
private void MultiThreadedcs_Load(object sender, EventArgs e)// Form Load
{
t1 = new Thread(Progressor1);
t2 = new Thread(Progressor2);
}
At run time it shows me following error:
Error Name = InvalidOperationException Error Message = Cross-thread operation not valid: Control 'progressBar2' accessed from a thread other than the thread it was created on.
Please help me out with this. I have less idea of threading concepts, i have gone through this solution with no progress in my problem: