Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have an application like this:
Form 1 => main form. When it loads it must update some information from database and read some files from disk
Form 2=> secondary form

I want to display Form 2 with a progress bar on it while the form1 updates its information and shows up his dialog and then hide form2

I put this code in Form2 class : BackgroundWorker and ProgressBar demo[^]
and i put in Form1 LOAD => Form2 f2=new Form2(); f2.Show()

But i don't get any progress in the progress bar...

C#
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
           // Your background task goes here
           for (int i = 0; i <= 100; i++)
           {
               // Report progress to 'UI' thread
               backgroundWorker1.ReportProgress(i);
               // Simulate long task
               System.Threading.Thread.Sleep(150);
           }
       }


C#
private void Form1_Load(object sender, EventArgs e)
       {


           this.CenterToScreen();
           users = usrsrv.ListaUtilizatori();//next 6 lines communicate with database and adds information in 6 lists
           articles = usrsrv.ListaArticole();
           tests = usrsrv.ListaTeste();
           userarticles = usrsrv.ListaArticoleUtilizator();
           usertests = usrsrv.ListaTesteUtilizator();
           fb = usrsrv.ListaFeedback();
           existentarticles = usrsrv.IncarcareNumeFisiereExistente();//reads from file

           int i = 0;
           foreach (utilizator util in users)
           {
               ut.Insert(i,util.username);
               i++;
           }
           this.ActiveControl = textBox1;

       }
Posted
Updated 5-Apr-13 0:49am
v2
Comments
Pheonyx 5-Apr-13 6:16am    
The issue you are having is because you want the progress bar to appear on Form 2, yet the background worker, and in turn its "Progress Changed" event handling code is all on Form 1.

You are better off not having form 2 and having an overlay progress bar appear in form 1.
Luci C 5-Apr-13 6:25am    
modified as you said...but form1 still doesn't show up only when it has done updating information
Pheonyx 5-Apr-13 6:39am    
That could be because you are processing it in the form load event. The form does not display until this event has completed.
Luci C 5-Apr-13 6:43am    
but isn't there the right place to put the code? I don't know other place where to put it..
Pheonyx 5-Apr-13 6:46am    
You could try the form shown event?
I'm not 100% sure. Without seeing your dowork event and your current form load event it's quite hard to speculate.

Could you use the "improve question" feature and add some code snippets please?

1 solution

In your Form Load event, wite the following code...

backgroundWorker1.RunWorkerAsync(); -- backgroundWorker1 control should be added to Form1
Form2 form2 = new Form2();
form2.Show();
-- progressBar1 is a control in Form2

In the DoWork event of BackGroundWorker...

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
--write your code to read files from disk...
}

In the RunWorkerCompleted Event of BackGroundWorker
{
--write code which should be done after reading files and
form2.Close();
}
 
Share this answer
 

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