473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

A better progressbar

Hi, thanks for the previous advice on progressbars with showing the
percentage. I would now like to create a less clunky version.

I.e at the moment my progressbar shows 7 separate steps. Some steps can take
seconds to process, but others could take upto 10 minutes (leaving the users
thinking nothing is happening!).

Is there a way to show the progressbar smoothly increasing until the final
step has finished instead of clunking it's way across?

Nov 16 '05 #1
2 6268
jez123456,

You could, but in order to do that, you would need to have an estimation
of when the step was going to complete. Once you know that, you can
basically divide the time by the number of pixels one step is, and then
every time that amount of time elapses, write one more bar with a length of
1 pixel to the progress bar.

For example, if you know it will take 10 seconds to complete the current
step, and the current step on the progress bar is five pixels wide, draw the
progress bar which is one pixel wider than the current every two seconds.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
Hi, thanks for the previous advice on progressbars with showing the
percentage. I would now like to create a less clunky version.

I.e at the moment my progressbar shows 7 separate steps. Some steps can
take
seconds to process, but others could take upto 10 minutes (leaving the
users
thinking nothing is happening!).

Is there a way to show the progressbar smoothly increasing until the final
step has finished instead of clunking it's way across?

Nov 16 '05 #2
Right. You can't make your progress bar truly smooth unless you know
exactly how long the entire operation is going to take.

You can, however, make those long parts of the operation appear to be more
responsive. My solution for this type of problem is to have the class that
encapsulates each step expose a "NumSteps" quantity. Before you start the
operation, you query each step for it's NumSteps, and add them all up. Then
set your ProgressBar.Start to 0, and ProgressBar.End to that total amount.
Now here's the important part. The classes that encapsulate each step each
have to define an event that they can use to report on their own progress.
You can make a single event handler in your main class to handle all of
those events, in which you just increment your ProgressBar.Value. Something
like this:

namespace myApp {
// A delegate for all those "intermediate progress" events:
public delegate void StepProgress(object sender, EventArgs e);

// three classes for three steps:
class A {
public event StepProgress AProgress;
public int NumSteps() { return 5; }
public void DoSomeWork() {
for(int i = 0; i < 5; i++) {
// blah blah blah
if(AProgress != null)
AProgress(this, (EventArgs)null);
}
}
}
class B {
// just like A, except that NumSteps is 12, and
// the DoSomeWork() method will invoke BProgress 12 times
}
class C {
// just like A, except that NumSteps is 2, and
// the DoSomeWork() method will invoke CProgress 2 times
}

class myMainClass {

public void HandleStepProgressEvents(object sender, EventArgs e) {
myProgressBar.Value++;
}

private void DoTheLongOperation() {
// create the worker objects.
A myA = new A();
B myB = new B();
C myC = new C();
// subscribe to their step events
A.AProgress += new StepProgress(HandleStepProgressEvents);
B.BProgress += new StepProgress(HandleStepProgressEvents);
C.CProgress += new StepProgress(HandleStepProgressEvents);
// set up my progress bar.
int TotalSteps = myA.NumSteps() + myB.NumSteps() + myC.NumSteps();
myProgressBar.Start = 0;
myProgressBar.Value = 0;
myProgressBar.End = TotalSteps;

A.DoSomeWork(); // presumably these methods spawn their own
threads
B.DoSomeWork(); // to do the actual work, otherwise you'll freeze
your UI
C.DoSomeWork(); // while the work is taking place.
}
}

}

So in this example, rather than only displaying 3 increments--one for A, one
for B, and one for C--you get 19 steps: 5 for A, 12 for B, and 2 for C.
This is still not perfect, as there's no real way to make sure each little
step is roughly the same amount of time, but at least it's better. And if
you have a step that might take 10 minutes, you can always have that event
report more steps back to the UI. Maybe 100, so you get a progress bar
update every 6 seconds.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ez*************@TK2MSFTNGP15.phx.gbl...
jez123456,

You could, but in order to do that, you would need to have an
estimation of when the step was going to complete. Once you know that,
you can basically divide the time by the number of pixels one step is, and
then every time that amount of time elapses, write one more bar with a
length of 1 pixel to the progress bar.

For example, if you know it will take 10 seconds to complete the
current step, and the current step on the progress bar is five pixels
wide, draw the progress bar which is one pixel wider than the current
every two seconds.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
Hi, thanks for the previous advice on progressbars with showing the
percentage. I would now like to create a less clunky version.

I.e at the moment my progressbar shows 7 separate steps. Some steps can
take
seconds to process, but others could take upto 10 minutes (leaving the
users
thinking nothing is happening!).

Is there a way to show the progressbar smoothly increasing until the
final
step has finished instead of clunking it's way across?


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Maka Sili | last post by:
Using ProgressBar.Enabled = false does not dim the progress bar. There must be a way. I hope somebody could guide me. Thanks.
2
by: Thomas Kehl | last post by:
Hi! Does anyone have a tipp for me where can I found (or how can I programm) a "progressbar" which has a gradient and the color go from the left side to right and back ... I should have a...
3
by: Steve Teeples | last post by:
Is there a way for code from one class of C# to send a communication to a progressbar in another class to update the bar during runtime? -- Steve
8
by: needin4mation | last post by:
Please consider: foreach (ListViewItem item in listViewFiles.Items) { // Display the ProgressBar control. pBar1.Visible = true; // Set Minimum to 1 to represent the first file being copied....
1
by: Mehr H | last post by:
I've been trying to figure out how i can embed a Windows.Forms.ProgressBar in my webform (aspx) file. I have tried putting a Windows.Forms.ProgressBar as public on a regular winform designer form...
3
by: Mitchell Vincent | last post by:
In other programming languages I've been able to easily change the style of a progress bar between smooth and blocked. I find that is either really hidden or impossible in .NET. Am I missing...
1
by: nobody | last post by:
Hi I'm currently developing a Windows application. At the start of the application I load several tables into datatables in a dataset. I also use a progressbar to show the user how much percent...
2
by: =?Utf-8?B?QWFyb24=?= | last post by:
Since some controls such as the DataGridView take a long time to update themselves when performing certain tasks, I have added a StatusStrip with a ProgressBar on it. While I am updating the...
4
by: sivamoorthy | last post by:
how to use a progressbar in a one cpp file but defined in another header file. the function in which i am using is a static member function. how to use the progressbar inside the function ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.