views:

184

answers:

1

This is XAML with a button and 3 progress bars

TestBtn

this is the thread to update the 3 progress bars..

Thread thread = new Thread(
                                new ThreadStart(
                                    delegate()
                                    {
                                        progressBar1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                                                        new Action(
                                                                                            delegate()
                                                                                            {
                                                                                                int x=0;
                                                                                                while(true)
                                                                                                {

                                                                                                    x++;
                                                                                                    Thread.Sleep(50);
                                                                                                progressBar1.Value = x;
                                                                                                    if (x>100)
                                                                                                    {
                                                                                                        break;
                                                                                                    }
                                                                                                }
                                                                                                MessageBox.Show(" Complete ");
                                                                                            }
                                                                                    ));


                                        progressBar2.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                                                        new Action(
                                                                                            delegate()
                                                                                            {
                                                                                                int y = 0;
                                                                                                while (true)
                                                                                                {
                                                                                                    Thread.Sleep(50);
                                                                                                    y = y + 2;
                                                                                                    progressBar2.Value = y;
                                                                                                    if (y > 100)
                                                                                                    {
                                                                                                        break;
                                                                                                    }
                                                                                                }
                                                                                                MessageBox.Show(" Complete ");
                                                                                            }
                                                                                    ));



                                        progressBar3.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                                                       new Action(
                                                                                           delegate()
                                                                                           {

                                                                                               int z = 0;
                                                                                               while (true)
                                                                                               {
                                                                                                   Thread.Sleep(50);
                                                                                                   z = z + 3;
                                                                                                   progressBar3.Value = z;
                                                                                                   if (z > 100)
                                                                                                   {
                                                                                                       break;
                                                                                                   }
                                                                                               }
                                                                                               MessageBox.Show(" Complete ");
                                                                                           }
                                                                                   ));
 )
            );//end of thread

            thread.Start();

        }

when i run this, all the 3 progress bars are updated to 100 percent at last(all are reaching directly to 100 at the same time) and it is not showing any increments from 0 to 100.

What i need is a thread with 3 item , where each will update the UI at the same time.So here at the same time all these 3 progress bars have to start and they have finish according to the loop.. (When we are copying files, in the UI , copying description is changing at the same time progress bar has to be updated.I want similar to that.So i think if i can understand a small example like this, then i can sort the required one)

pls correct me, where i cam wrong. Thnks Kaja

+1  A: 

You could always use a BackgroundWorker, example by Figo Fei (altered slightly):

private void button1_Click(object sender, EventArgs e)
{
    progressBar1.Maximum = 100;
    backgroundWorker1.WorkerReportsProgress = true;
    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
    backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // This would be the load process, where you put your Load methods into.
    // You would report progress as something loads.

    for (int i = 0; i < 100; i++)
    {
        Thread.Sleep(100);
        backgroundWorker1.ReportProgress(i); //run in back thread
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) //call back method
{
    progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) //call back method
{
    progressBar1.Value = progressBar1.Maximum;
}

One of the lovely thing about the BackgroundWorker is that it does run all the "heavy lifting" on a worker thread, but reports back to the UI thread. So by using the above code to your own needs, you should solve your problem.

Kyle Rozendo
Hai Kyle,Thank you for your help..i could able to find the solution for your problem with this one...:)Kaja
Kaja
A pleasure Kaja, if this does indeed work for you, please remember to mark this as the answer. Cheers, Kyle.
Kyle Rozendo