如何测量应用程序加载时间?

发布于 2024-08-08 23:52:36 字数 1269 浏览 4 评论 0原文

我想知道当用户启动进程、应用程序实例时如何测量应用程序加载时间,以便我可以显示进度条或通知用户应用程序加载时发生的情况或应用程序加载量的内容完全的。

我的意思是,如果我想用进度条显示当前进度,那么我想我可以用数字定义当前进程,这样我就可以增加 ProgressBarValue 属性代码>控制。

提前致谢。

真挚地。

编辑:

我发现的解决方案是:

您可以使用 系统.诊断.秒表 测量时间。致电 方法从开头开始 表单的构造函数。

显示表单后, 通常是 Application.Idle 事件 上升。因此,您可以调用 Stop 该事件的处理程序中的方法。 但你应该检查一下这个事件 确实会上升,例如通过使用 系统.诊断.调试.WriteLine, 与 DebugView 工具一起使用 sysinternals.com。

所以我们可以像这样使用 System.Diagnostics.StopWatch :

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");
    }
}

然后当 Idle 事件触发时,我将能够找到加载时间并将其显示在进度条上,但进度条不会显示准确的时间我认为加载时间的百分比。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

并安 2024-08-15 23:52:36

您的进度条不太可能真正指示剩余时间。一种常见的方法是使用完成的“步骤”数来完成加载。

假设您必须运行 3 个方法,并且每个方法还有 2 个子方法。这就是 6。使用 6 个步骤的进度条。如果您基于尝试错误,请确定哪个“步骤”可能需要更长的时间,并为它们分配更多的“步数”

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'

沉默的熊 2024-08-15 23:52:36

这取决于。

您的应用程序正在加载什么?为什么需要时间?

一种选择是跟踪启动程序需要多长时间,并使用该持续时间来设置将来的进度条。 (这应该在运行时完成并存储在某个地方,因为它会随着机器的速度而变化)

对于更具体的答案,请告诉我们您正在加载的内容。

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.

骄兵必败 2024-08-15 23:52:36

通常,此示例中的进度条不是加载时间的函数,而是加载任务的函数。

您需要查看应用程序在启动时所做的工作,并为每个步骤分配百分比值。这些值不需要是任意的。相反,在您自己的计算机上执行一些指标来确定每个任务通常需要多长时间,然后使用它来确定您的百分比。

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.

你在看孤独的风景 2024-08-15 23:52:36

我刚刚问了一个非常类似的问题,但我没有使用进度条,而是使用标签来显示负载百分比。过去,ProgressBar 非常不可靠。您可以在这里找到我的帖子:

C# - 显示加载 1-100% 内4秒

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

一念一轮回 2024-08-15 23:52:36

您应该具有在程序启动时检查程序中的记录或值的功能。然后只需使用计时器以及timer_Tick()的属性函数将您的函数放入timer_tick()中,然后只需要所需的时间即可打开您的程序。

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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文