Click here to Skip to main content
11,703,617 members (66,022 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# progress bar
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...

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);
           }
       }

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 5-Apr-13 0:10am
Luci C215
Edited 5-Apr-13 0:49am
v2
Comments
Pheonyx at 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.
Member 8708121 at 5-Apr-13 6:25am
   
modified as you said...but form1 still doesn't show up only when it has done updating information
Pheonyx at 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.
Member 8708121 at 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 at 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?
Member 8708121 at 5-Apr-13 6:51am
   
I didn't modify the dowork event at all...I took it from the example
Pheonyx at 5-Apr-13 6:53am
   
Then you are not doing anything on the separate thread! The DoWork method should be doing all the data processing that is locking the main thread.

As a result, the main thread is being locked while it is attempting the long running operation, resulting in your not seeing anything until it all finishes.

I highly recommend you read up more on multi-threading and the background worker classes.

From what I can see, you don't even appear to instantiate the background worker at all?
Member 8708121 at 5-Apr-13 7:06am
   
thank you...I have succeeded:
1.moved all the code with background worker and progress bar to form 2
2.start up form for the project is form 2
3.put all the data processing in DoWork method
4.removed unnecessary code from form1_load event (so that it show up quickly)
5. when background worker completes all the work, I hide form2, I transmit all the lists i need as a parameter to form1, and finally show form 1 dialog

Thank you for patience and explanations
Vignesh Theyagarajan at 5-Apr-13 7:07am
   
I think my solution might help your issue... Pls check and confirm
Member 8708121 at 5-Apr-13 7:10am
   
I have done something like you posted...It's ok, I accept your answer as the good one
Thank you for helping
Vignesh Theyagarajan at 5-Apr-13 7:19am
   
Pleasure is all mine...:)
Pheonyx at 5-Apr-13 7:09am
   
I'm pleased my help has allowed you to solve the issue :)

It is something I struggled with for a while when I first tried it so I understand the frustration.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 550
1 Maciej Los 530
2 Sergey Alexandrovich Kryukov 381
3 Andy Lanng 294
4 CPallini 195
0 OriginalGriff 8,744
1 Sergey Alexandrovich Kryukov 7,747
2 CPallini 5,107
3 Maciej Los 4,716
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100