Click here to Skip to main content
13,088,439 members (56,860 online)
Rate this:
Please Sign up or sign in to 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...

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
           // Your background task goes here
           for (int i = 0; i <= 100; i++)
               // Report progress to 'UI' thread
               // Simulate long task

 private void Form1_Load(object sender, EventArgs e)
            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)
            this.ActiveControl = textBox1;
Posted 5-Apr-13 0:10am
Luci C250
Updated 5-Apr-13 0:49am
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.
Member 8708121 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.
Member 8708121 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?
Member 8708121 5-Apr-13 6:51am
I didn't modify the dowork event at all...I took it from the example
Pheonyx 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 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 5-Apr-13 7:07am
I think my solution might help your issue... Pls check and confirm
Member 8708121 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 5-Apr-13 7:19am
Pleasure is all mine...:)
Pheonyx 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
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();
-- 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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2017
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