Progress Bar in a Winform application , Increment Progress Bar till a query is executed on a database
I am connecting to a mysql database online from my local C# .NET Winform application. I want that till the application connects to the online database, a Progress Bar is displayed , and as soon as the database connection is established, the progress bar should also complete incrementing.
please help with code. how do i g开发者_JAVA百科et the time taken by the application to connect to the online database , and then set that time as INTERVAL for the progress bar?
Why don't you change ProgressBar Style to marque a line before Query Start's Execution ,than on end you can Change ProgressBar Style in Block's and Give Progressbar.Value = 100;
.
If i read that correctly, you would like to have a progress bar continually fill over and over until a connection with the database is established (since it is unknown how long a connection could take prior to completion)
If this is the case you can use a timer/backgroundworker to do this pretty easy. Sudo code:
Timer a= new Timer();
a.Tick += TickMethod;
a.interval = 2;
BackgroundWorker b = new BackgroundWorker();
b.DoWork += BackgroundMethod();
b.WorkComplete += WorkDone();
void Start()
{
a.Start();
b.RunAsync();
}
void TickMethod()
{
if(progressBar.Value == progressBar.Max)
progressBar.Value = 0;
progresssBar.Step();
}
void BackgroundMethod(object s, Args e)
{
MakeConnection();
}
void WorkDone()
{
a.Stop();
progressBar.Value = progressBar.Max();
}
I'm sorry if thats not what you are looking for.
The "proper" way to use a progress bar for an operation that will take an indetermine time is to set it into Marquee mode. This shows the bar with an animated "shine" effect to show that something is happening, but does not show an actual progress bar (as you don't know how long it will take, you can't estimate a percentage complete). Once you have connected, you can then set the progressbar's
Value
to itsMax
so that it moves to 100%.An alternative approach was used in some versions of IE while fetching web pages. This showed a progress bar that slowly incremented towards 100% over a long period (a predetermined time frame long enough to cover the worst case scenario - e.g. in your case you might choose 30 seconds as that is often the timeout period used to fail a connection anyway). When the connection is finally made, you then advance the bar to 100%. THis gives the impression of progress being made while you're waiting, and as long as your estimate of the worst case scenarion is reasonable, this will give good results.
However, note that .net progress bars don't jump immediately to a new value - they animate so as to "grow" towards the requested value, so if it has to go from 0% to 100% it will take a second or two before it actually displays 100%. This can mean that (for example) your window closes while the progress bar is only showing a value like 20%, which is highly irritating as it makes the user think that it failed in some way. There are two solutions to this:
1) Make your application pause (e.g. Sleep the thread) for a "long enough period" that you are sure the progress bar will reach 100%. This adds an extra delay of several seconds to the end of your operation, and the time needed can be rather unpredictable, so it's not a great solution, especially if the database fetch is often quick.
2) Force the progress bar to "jump" to 100% rather than slowly growing towards 100%. You can do this by setting Value = Max; Value = Max-1; Value = Max
. When the progress bar is asked to go "backwards" in this way (to Max-1) it doesn't animate, but immediately updates to show the requested value.
精彩评论