获得程序正常运行时间

发布于 2025-01-02 11:55:15 字数 449 浏览 4 评论 0原文

我是编程初学者。这是我的代码:

private void timer1_Tick(object sender, EventArgs e)
{
    sec++;
    textBoxSeconds.Text = sec.ToString();
    if (sec > 59)
    {
        sec = 0;
        textBoxSeconds.Text = sec.ToString();
        min++;
        textBoxMinutes.Text = min.ToString();
    }
}

时间过得太快;/有时会停止几秒钟。 希望有人能帮助我:) *编辑//* 感谢您的帮助:)它可以工作,但我仍然有一个我之前没有提到的问题。时间有时会停止 1-2 秒,不知道为什么。也许是因为一些循环?

I'm beginner to programming. This is my code:

private void timer1_Tick(object sender, EventArgs e)
{
    sec++;
    textBoxSeconds.Text = sec.ToString();
    if (sec > 59)
    {
        sec = 0;
        textBoxSeconds.Text = sec.ToString();
        min++;
        textBoxMinutes.Text = min.ToString();
    }
}

time goes too fast;/ and it stops for few sec sometimes. Hope someone can help me:)
*EDIT//*
thanks for help:) it works, but i still have a problem i didnt menton earlier. time stops sometimes for 1-2 sec, idk why. maybe because of some loops?

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

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

发布评论

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

评论(3

自演自醉 2025-01-09 11:55:15

这是错误的做法。当您的程序启动时,只需保存一个DateTime实例,即startTime。在计时器刻度处理程序中,计算当前时间和开始时间之间的差异并显示该差异。

private DateTime startTime = DateTime.Now;

private void timer1_Tick(object sender, EventArgs e)
{
    var delta = DateTime.Now - startTime;
    textBoxSeconds.Text = delta.Seconds.ToString("n0");
    textBoxMinutes.Text = Math.Floor(delta.TotalMinutes).ToString("n0");
}

This is the wrong approach. When you program starts just save a DateTime instance, i.e. startTime. In your timer tick handler calculate the difference between the current time and the start time and display that.

private DateTime startTime = DateTime.Now;

private void timer1_Tick(object sender, EventArgs e)
{
    var delta = DateTime.Now - startTime;
    textBoxSeconds.Text = delta.Seconds.ToString("n0");
    textBoxMinutes.Text = Math.Floor(delta.TotalMinutes).ToString("n0");
}
山人契 2025-01-09 11:55:15

使用您的代码,我可以说您可能尚未设置计时器 Interval,因此:

timer1.Interval = 1000; //1000 毫秒 = 1 秒

然后您可以改进 Tick 事件中的某些内容:

private void timer1_Tick(object sender, EventArgs e)
{
    sec++;

    if (sec == 60)
    {
        sec = 0;
        min++;
    }

    textBoxSeconds.Text = sec.ToString();
    textBoxMinutes.Text = min.ToString();
}

因此使用 DateTime 类,这是最好的解决方案。

编辑:

    DateTime startTime = DateTime.Now;

    void timer1_Tick(object sender, EventArgs e)
    {
        TimeSpan time = DateTime.Now - startTime;
        textBoxSeconds.Text = string.Format("{0:0#}", time.Seconds);
        textBoxMinutes.Text = string.Format("{0:0#}", time.Minutes);
    }

Using your code, I can say probably you haven't set the timer Interval, so:

timer1.Interval = 1000; //1000 ms = 1 second

Then you can improve something in the Tick event:

private void timer1_Tick(object sender, EventArgs e)
{
    sec++;

    if (sec == 60)
    {
        sec = 0;
        min++;
    }

    textBoxSeconds.Text = sec.ToString();
    textBoxMinutes.Text = min.ToString();
}

So use the DateTime class, it's the best solution.

EDIT:

    DateTime startTime = DateTime.Now;

    void timer1_Tick(object sender, EventArgs e)
    {
        TimeSpan time = DateTime.Now - startTime;
        textBoxSeconds.Text = string.Format("{0:0#}", time.Seconds);
        textBoxMinutes.Text = string.Format("{0:0#}", time.Minutes);
    }
抱着落日 2025-01-09 11:55:15

我同意 startTime - 这是强制性的。我还评论了 DataTime.UtcNow - 这是正确的方法。

关于 1..2 秒延迟的第二个问题 - 这是因为计时器的滴答声与秒滴答声并排运行。

1) 如果您的计时器将在 998 毫秒而不是 1000 毫秒内触发,您可以读取相同的秒数,并且该数字将保留在下一个时钟周期之前。

2)因为从操作系统的角度来看,应用程序不是实时优先级的,所以它可以保留几秒钟(例如,用于由其他应用程序渲染多媒体内容),并且您可以注意到跳过了1秒......

要解决第一个原因并通过将间隔减少到 500 或 333 来促进第二次尝试增加滴答计数。

对于保留资源的更高级策略,您仍应使用 1000 毫秒,但使用 dateTime.Milliseconds 定期与每半秒交叉同步计时器。这将最大限度地避免并排赛车问题的可能性,而无需额外的滴答声。

I agree about startTime - it is mandatory. I've also commented about DataTime.UtcNow - this is correct way.

About your second problem with 1..2 seconds lag - this is because timer's ticks racing side by side with seconds ticks.

1) If your timer will be triggered in 998ms instead of 1000ms, you can read the same amount of second and this number will stay before next tick.

2) Because application is not in real-time priority from OS point of view, it can be held for several seconds (e.g. for rendering multimedia stuff by other app) and you can notice a skip of 1 second...

To solve 1st reason and facilitate 2nd try to increase ticks count by decreasing Interval to 500 or 333.

For more advanced strategy that preserves your resources, you should still use 1000ms but synchronize you timer periodically with each half second crossing using dateTime.Milliseconds. That will maximize probability of avoiding side-by-side racing problem without extra ticks.

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