使用计时器C#停止观看延迟 [英] Stop watch delay using timer c#

查看:88
本文介绍了使用计时器C#停止观看延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在秒表中添加一个秒表,但我的秒表落后于PC中其他计时器的时间。您能为此提供任何解决方案吗?
(我的计时器间隔是100)。
这是我的代码:

I want to add a stopWatch to my form but the one i made is lagging behind the time of other timer in pc. Can you give any solution for that? (My timer interval is 100). this is my code:

int min, sec, ms = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        Time.Text = min + ":" + sec + ":" + ms.ToString();
        ms++;
        if (ms > 9)
        {
            ms = 0;
            sec++;
        }

        if (sec > 59)
        {
            sec = 0;
            min++;
        }

    }

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

它可以正常工作,但延迟很容易。我已经尝试更改了很多东西,但没有一个起作用。

It working but with easy delay. I have tried to change so many things but none of them is working..

推荐答案

计时器的刻度不如您想象的那么精确。因此,最好计算开始时间和调用时间 tick 之间的时间差。

Timer's tick is not as precise as you think. So It would be better if you calculate the difference between start time and the time tick invoked.

DateTime startTime = DateTime.Now;  
private void timer1_Tick(object sender, EventArgs e)
{
    Time.Text = DateTime.Now.Subtract(startTime).ToString(@"mm\:ss\.ff");
}

private void timer_Click(object sender, EventArgs e)
{
    startTime = DateTime.Now;   
    timer1.Start();
}

这篇关于使用计时器C#停止观看延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆