0

I'm having an application with a progress bar and a buttom.

When the button clicked the progress bar value will get increased, here is the source code,

private void Run()
        {               
                progressBar1.Maximum = 1000;
                progressBar1.Minimum = 0;
                progressBar1.Step = 1;

                for (int l_nIndex = 0; l_nIndex < 1000; l_nIndex++)
                {
                        progressBar1.Value++;
                        Thread.Sleep(10);
                }
            }


private void button1_Click(object sender, EventArgs e)
        {
                Run();
    }

so when i run the application, the progress bar value is getting increased, but when i try to move the window its not responding.

I can not run it in a normay thread way - it will throw Cross-Thread error.

so i changed the code like,

private void Run()
        {
                if (this.InvokeRequired)
                {
                    this.Invoke(new MethodInvoker(this.Run));
                }
                else
                {
                    progressBar1.Maximum = 1000;
                    progressBar1.Minimum = 0;
                    progressBar1.Step = 1;
                    for (int l_nIndex = 0; l_nIndex < 1000; l_nIndex++)
                    {
                            progressBar1.Value++;
                            Thread.Sleep(10);
                    }
                }
        }

 private void button1_Click(object sender, EventArgs e)
        {
                Thread myThread = new Thread(new ThreadStart( Run));
              myThread.Start();
        }

Now i can able to move the winodow, but when i move the progress bar is stopped, and when i release the mouse button its resuming. So still the execution is in UI Thread. How to handle it in a better way.Please help me to do this .

Thorin Oakenshield
  • 14,232
  • 33
  • 106
  • 146
  • See my answer here: [Answer to "Cross Thread operation failed"][1] [1]: http://stackoverflow.com/questions/1489374/cross-thread-operation-not-valid-does-not-kill-winforms-application-and-it-sh/1489452#1489452 – Spence Nov 02 '11 at 04:43
  • 1
    Everything here is wrong, i cannot do anything about it. Everything. Why you don't just use a Timer control? – Salvatore Previti Nov 02 '11 at 04:43

2 Answers2

3

Invoke() works by running the given delegate from the UI thread. So if you use Invoke() to run your entire method, then your entire method runs from the UI thread.

Instead, you should be doing your actual work in the other thread, and just performing UI updates in the UI thread, by just Invoke()ing the little bits of code that perform the updates.

One easy way to do this is to use the BackgroundWorker class built into the standard library.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
3

This has been answered here - in your case the code should look something like:

this.BeginInvoke((Action)(() => progressBar1.Value++));
Community
  • 1
  • 1
Veldmuis
  • 4,678
  • 4
  • 25
  • 25