2

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:

c# threading access to other thread

Community
  • 1
  • 1
Zaki Mohammed
  • 969
  • 14
  • 24
  • 2
    As explained in the linked question, you can't access UI elements from other threads. It's not clear what your aim is - there's no point to your threads at the moment. You should do *work* on a background thread and update the UI on the UI thread. I'd also look at TPL/async/await - you shouldn't be using `Thread` directly. – Charles Mager Jun 09 '15 at 07:10
  • If i make the threads as background thread does it solve my problem? – Zaki Mohammed Jun 09 '15 at 07:13
  • @ZakiMohammed No. Your problem is that you're accessing the UI from threads other than the UI thread - solving this really depends on what you're *actually* trying to do - your code doesn't really make any sense. – Luaan Jun 09 '15 at 07:14
  • Did you try searching 'Cross-thread operation not valid' before posting question? – Sergey Berezovskiy Jun 09 '15 at 07:15
  • @CharlesMager - People who have problems with the concept of threading usually have problems with understanding which chunk of code runs in which thread context. They usually _falsely_ assume that _callbacks_/_delegates_/_methods of a class instance created in the main thread_ will run in the GUI thread "because the code is there". I think this case is a fine example of that. – mg30rg Jun 09 '15 at 07:47
  • Actually i was trying to do this because as in the console based application with no GUI in that such simultaneous task appears to be normal, i haven't tried with the UI based application although i have seen that answer which is there in the above link but it seems little bit difficult as for now to grasp that much i think i have to dig more in this topic before asking already asked question. Any ways thanks. – Zaki Mohammed Jun 09 '15 at 09:29

1 Answers1

0

In the methods you've written to start as new threads you're calling the progress bar directly, so you're trying to edit data from another (the original) thread. Instead of directly editing the progress bar's value, you should Invoke a method which does, like so:

for (int j = 0; j <= 100; j++)
  this.progressBar1.Invoke((Action) () => this.progressBar1.Value = j, null);
Melvin
  • 344
  • 1
  • 8