views:

449

answers:

4

Hi everyone,

I am wondering how to measure an application Loading Time when user starts the process,application instance so that I can show a progress bar or something which informs users what's happening when application is loading or how much application loading is completed.

I mean what if I want to show current progress with a progress bar so I think I am able to define the the current process with numbers so I can increase the Value property of an ProgressBar control.

Thanks in advance.

Sincerely.

Edit :

What I've found as a solution is :

You can use System.Diagnostics.Stopwatch for measuring time. Place a call to the method Start at the beginning of the constructor of the form.

After a form has been displayed, typically the Application.Idle Event rises. Thus, you could call the Stop method in a handler for this event. But you should check that this Event indeed does rise, e.g. by using System.Diagnostics.Debug.WriteLine, together with the tool DebugView from sysinternals.com.

So we can use System.Diagnostics.StopWatch like this :

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Thread.Sleep(10000);
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine(elapsedTime, "RunTime");
    }
}

And then when Idle event fires, I would be able to find the loading time and show it on the progress bar but progress bar would not show the accurate percentage of loading time, I think.

+1  A: 

It depends.

What is your application loading, and why is it taking time?

One option would be to track how long it takes to start the program, and use that duration to set the progress bar in the future. (This should be done at runtime and stored somewhere, because it will vary with the speed of the machine)

For a more specific answer, please tell us what you're loading.

SLaks
I think best way would be `System.Diagnostics.Stopwatch` method
Braveyard
@Aaron: You don't really need that level of precision - you could simply check the change in `DateTime.Now`. However, you certainly could use a `StopWatch`.
SLaks
+5  A: 

It's very unlikely that your progress bar will be a true indication of TIME remaining. A common approach is to use the number of 'steps' completed towards loading completed.

Say you have to run 3 methods and each one has 2 more sub-ones. That makes it 6. Using 6 steps progress bar. If you based on trial an error, determine which 'step' might take longer and assign them with more 'step count'

o.k.w
I'd say this approach is common because it's the only approach that can possibly work. Trying to guess how long the load will take is a dead-end (seems like I heard that somewhere not too long ago). :)
MusiGenesis
@MusiGenesis: Yea, unless we 'force' the loading time to be of X seconds, if it completes before X sec, stall it. That will be very 'unfriendly'. SOme machine might take more than X sec and the timing will be screwed. :P
o.k.w
One way is to have the app measure the startup time each time the app starts, and store a running average (like of the last 3 startup times, maybe) in the registry, and then use this as a guess of how long the current startup will take. This is just a lot of work for almost no payoff. I wish people would just write apps that don't take so damn long to start in the first place. Yes, iTunes authors, I'm talking to you.
MusiGenesis
@MusiGenesis: Sounds like a good 'framework' towards a accurate loading time estimation. :)
o.k.w
+1  A: 

Typically, progress bars in this example are not a function of loading time, but loading tasks.

You need to look at the work your application does on startup and assign percentage values to each step. The values don't need to be arbitrary. Instead, do some metrics on your own machine to determine how long each of these tasks typically take, and then use that to determine your percentages.

Cam Soper
+1  A: 

I just asked a very similar question, but instead of using a ProgressBar, I am using a label to display the load percentage. In the past, ProgressBars have been very unreliable. You can find my post here:

http://stackoverflow.com/questions/1604302/c-display-loading-1-100-within-4-seconds

Nate Shoffner