Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code doesn't execute properly.

I need to run this progress bar, but in run time it doesn't increase the value.
Please help me!

C#
if (progressBar1.Value < progressBar1.Maximum)
      progressBar1.Value = progressBar1.Value + 1;


If I use "while" instead of "if" then the progress bar works properly, but the bar fills up so quickly.

How can I slow down the speed of filling the progressBar?


There are 2 applications running on 2 laptops.
One application passes a value to the other.
Application1 pass a no to application2, and when application2 receives the no from application1, variable state is set to 0 (variable "state" is in the applicatio2)otherwise state is set to 1
(variable "state" is set to 1 until it receives the no from application1,as soon as application2 receives the no from application1 variable "state" is changed to 0 ).

The progress bar should run in application2 until it receives the no fromapplication1(the progress bar in application2 should run until the variable "state" changes from 1 to 0)
(simply the progress bar should run as long as "state" is equal to 1)
Posted
Updated 3-Oct-11 21:39pm
v3
Comments
Karthik Harve 4-Oct-11 2:46am    
Normally We use progress bar to have an indication about the task completion. If the progress fill up fastly means task is getting completed. Can you please mention your actual requirement..? if u want to have some delay, put some empty loop which loop value as 1000 or above..
lukeer 4-Oct-11 3:00am    
I beg you, please, don't build empty loops. If you are really that desperate, then even Sleep() would be better.
Simon Bang Terkildsen 4-Oct-11 3:27am    
To use use Thread.Sleep you have to be really, really, really desperate :)
Firo Atrum Ventus 4-Oct-11 2:54am    
The code that you posted is too few. Please post the whole code that are meant to show the progress bar.
Member 7779792 4-Oct-11 3:16am    
question is updated

Normally, a progress bar is used to indicate how far a task has progressed, and thus to give the user an idea how long it will take to complete.

If you replace the if with while:
C#
while (progressBar1.Value < progressBar1.Maximum)
      progressBar1.Value = progressBar1.Value + 1;
then it is effectively executed as a single statement:
C#
progressBar1.Value = progressBar1.Maximum


What you would normally do it to start the progress bar at the beginning of a task, and increase the value each time you had done some significant part of the work. For example, if you were processing ten files, the the maximum value might be ten, and you would increase the Value property after each file was processed.

However, if you are using a progress bar, then normally the processing is done in a separate thread, to get the User Interface nice and fluid - the only problem being that you need to Invoke controls in the processing thread as it cannot access UI controls.
If you use a System.ComponentModel.BackgroundWorker, there's no need for you to do the invoking.
The BackgroundWorker offers status reporting across thread bounderies on its own.
1. Set the BackgroundWorker.WorkerReportsProgress property to true.
2. Within the worker routine call ((BackgroundWorker)sender).ReportProgress().
3. Do the UI changes (including progress bar) within BackgroundWorker.ProgressChanged event handler.
 
Share this answer
 
Comments
Simon Bang Terkildsen 4-Oct-11 3:28am    
My 5
Is there any way to determine the time required for application1 to send something to application2? Because that's when progress bar is used: when you can estimate time or amount of operations. It shows the user which part of the whole work is done and how much the user has to wait.
If, however, your application2 can wait for application1 for undetermined amount of time, progress bar cannot be used. How can you tell the user that, say, 30% of the wait time has passed when you just don't know how much your program (and user) shall actually wait?
On the other hand, if you just want to show the user that your application is waiting right now, it changes the situation. There are controls that can be used, the simplest is actually the progress bar. It should fill up rather quickly, I guess, then become empty (set progress to zero), fill up again... and so on. In this case use the code similar to shown, just change if to while and put some delay inside - wait for event or, as lukeer suggests, Sleep(). If you perform "wait for application1" functionality in the same place with progress bar, consider updating progress bar in this "wait for application1" cycle (if any)
 
Share this answer
 
If you want to inform the user that something is going on at the moment, but you don't know exactly how long it is going to last, you can set
ProgressBar1.Style = ProgressBarStyle.Marquee;
This will show a nice animation without you needing to update anything.
 
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