开发者

How to measure an application loading time?

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 wi开发者_运维知识库th 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.


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'


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.


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.


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:

C# - Display loading 1-100% within 4 seconds


You should have function that checks the record or values in the program when the program get starts. Then just use the Timer along with the property function of timer_Tick() put your function into timer_tick() and then it takes only required time to open your program.

private void Button1_Click(object sender, EventArgs e)
{
    timer1.Start();


}

private void timer1_Tick(object sender, EventArgs e)
{
    ProgressBar1.Value += 1;

    if(ProgressBar1.Value==100)
    {
        timer1.Stop();
        Form1 f1 = new Form1();
        this.Hide();
        f1.Show();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜