【问题标题】:Display time in seconds by using timer使用计时器以秒为单位显示时间
【发布时间】:2013-11-22 11:31:31
【问题描述】:

我有一个间隔为 1 的计时器。每次计时,我想添加时间并将其显示在我的表单上。

但它有问题。时间更新自己的方式变慢。如果我将间隔设置为 1000,它可以工作,但我需要它运行得更快。

这是我的代码:

            private void button1_Click_1(object sender, EventArgs e)
    {
        timer.Interval = 1;
        timer.Start();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        lCount++;
        label1.Text = GetTimeForGUI(lCount);
    }

private String GetTimeForGUI(long lTimeInMilliSeconds)
    {
        String sGUITime = string.Empty;
        try
        {
            // Get Format: 00:00
            if (((lTimeInMilliSeconds / 1000) % 60) == 0)
            {
                sGUITime = Convert.ToString((lTimeInMilliSeconds / 1000) / 60);

                // Get the number of digits
                switch (sGUITime.Length)
                {
                    case 0:
                        sGUITime = "00:00";
                        break;   
                    case 1:
                        sGUITime = "0" + sGUITime + ":" + "00";
                        break;
                    case 2:
                        sGUITime = sGUITime + ":" + "00";
                        break;
                }
            }
            else
            {
                long lMinutes;
                long lSeconds;

                // Get seconds
                lTimeInMilliSeconds = lTimeInMilliSeconds / 1000;
                lSeconds = lTimeInMilliSeconds % 60;
                lMinutes = (lTimeInMilliSeconds - lSeconds) / 60;

                switch (Convert.ToString(lMinutes).Length)
                {
                    case 0:
                        sGUITime = "00";
                        break;
                    case 1:
                        sGUITime = "0" + Convert.ToString(lMinutes);
                        break;
                    case 2:
                        sGUITime = Convert.ToString(lMinutes);
                        break;    
                }

                sGUITime = sGUITime + ":";

                switch (Convert.ToString(iSeconds).Length)
                {
                    case 0:
                        sGUITime = sGUITime + "00";
                        break;
                    case 1:
                        sGUITime = sGUITime + "0" + Convert.ToString(lSeconds);
                        break;
                    case 2:
                        sGUITime = sGUITime + Convert.ToString(lSeconds);
                        break;
                }

            }

            return sGUITime;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return string.Empty;
        }
    }

【问题讨论】:

  • 这不是问题吗? if (((lTimeInMilliSeconds / 1000) % 60) == 0)
  • 什么时候更新很慢,间隔1还是1000?有点不清楚你在问什么。
  • 告诉我们您到底想要做什么?你想完成什么?
  • Uaaa 停止使用那个匈牙利符号……
  • 对于线程安全增量使用 Interlocked.Increment(lCount) 几乎 lCount++;

标签: c# timer


【解决方案1】:

我认为你自己做的太多了。使用该框架来帮助您。

当您有 Timer 和 DateTime 可用时,不要根据计数器计算您自己的秒数。

下面的实现将为您提供标签中的秒数(每 100 毫秒更新一次)

    DateTime startTime;
    private void button1_Click(object sender, EventArgs e)
    {
        timer.Tick += (s, ev) => { label1.Text = String.Format("{0:00}", (DateTime.Now - startTime).Seconds); };
        startTime = DateTime.Now;
        timer.Interval = 100;       // every 1/10 of a second
        timer.Start();
    }   

希望这会有所帮助,

【讨论】:

    【解决方案2】:

    1 表示 1 毫秒。当您完成 GUI 更新时,它会再次触发。这就是您面临延迟的原因。我不明白你到底为什么要每 1 毫秒更新一次。您需要清楚地发布您的要求以获得更好的答案。

    【讨论】:

      【解决方案3】:
      // Get seconds
      lTimeInMilliSeconds = lTimeInMilliSeconds / 1000;
      lSeconds = lTimeInMilliSeconds % 60;
      lMinutes = (lTimeInMilliSeconds - lSeconds) / 60;
      

      只要你生活在地球上并使用SI,一秒有 1000 毫秒,一分钟有 60 秒,一小时有 60 分钟,一天有 24 小时。

      如果您不喜欢基础数学,您应该考虑使用DateTimeTimeSpan

      【讨论】:

        猜你喜欢
        • 2018-03-27
        • 2017-03-16
        • 2022-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        相关资源
        最近更新 更多