Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hi

i want to use progress bar in my windows form application.

i tried using BackgroundWorker from this link http://eclipsed4utoo.com/blog/windows-form-progressbar/[^].

In my form on button click it downloads data from SQL database to XML file, it takes 8 to 10 sec for data downloading.in that time i would like to show progress bar.
here problem is progress bar is showing after code downloaded from SQL to XML.

here is the code below

C#
bgw = new BackgroundWorker();
     bgw.WorkerReportsProgress = true;
     bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
     bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
     bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);

       private void btnSubmitEventId_Click(object sender, EventArgs e)
        {
 
        bgw.RunWorkerAsync();  // called RunWorkerAsync in my button click

       // here my code for data downloading

        }



    void bgw_DoWork(object sender, DoWorkEventArgs e)
   {
    for (int i = 0; i < 10; i++)
    {
        bgw.ReportProgress((i + 1) * 10);
        Thread.Sleep(1000);
    }
   }

      void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
     {
        progressBar1.Value = e.ProgressPercentage;
     }

     void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
     {
     MessageBox.Show("Finished");
     }


here i understand that ReportProgress raises the bgw_ProgressChanged and assigned progress ercentage to  progressBar1.Value that is fine but here  ReportProgress raises bgw_ProgressChanged method after my data downloading is completed.

plz help me to find this out. 
Posted
Updated 23-Oct-13 7:26am
v2
Comments
Varun_nayak 20-Aug-14 7:44am    
I am facing Same problem.. Did u Found any Solutions ??

1 solution

Solution may be something like:
C#
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   progressBar1.BeginInvoke(new MethodInvoker(pb => pb.Value = e.ProgressPercentage));
}

Since you are trying to update an UI element (your ProgressBar) from a different thread than the one which has created it, you have to use BeginInvoke() method.

Hope this helps.

[edit] Updated to construct a delegate as parameter for BeginInvoke() [/edit]
 
Share this answer
 
v3
Comments
dorababu407 23-Oct-13 13:54pm    
hi phil.o,
thnks for rply.
here in BeginInvoke method i was unable to assign value to progressbar.
it was showing Error " Cannot convert Lambda expression to 'System.delegate' becoz it is non=delegate type"
phil.o 23-Oct-13 14:28pm    
Ok; I will update my answer to construct the delegate it needs.
dorababu407 24-Oct-13 6:45am    
i check my code using break point, i think here problem is progress bar is unable to show UI status when data downloading from SQL to XML. after data downloaded i write some code to excute(to kill sometime) at that time progress bar occurs shows.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900