【问题标题】:if specific time passed show messagebox in C#如果特定时间过去,则在 C# 中显示消息框
【发布时间】:2013-05-09 12:24:49
【问题描述】:

所以,我想要这个:如果从加载表单开始经过特定时间(例如 9 小时),那么我想显示消息框说“9 小时过去了”。我的代码是这样的:

public partial class Form1 : Form
{
    Stopwatch stopWatch = new Stopwatch();

    public Form1()
    {
        InitializeComponent();
        stopWatch.Start();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        double sec = stopWatch.ElapsedMilliseconds / 1000;
        double min = sec / 60;
        double hour = min / 60;
        if (hour == 9.00D)
        {
            stopWatch.Stop();
            MessageBox.Show("passed: " + hour.ToString("0.00"));
        }
    }
}

问题是我不知道在哪里写这部分代码:

if (hour == 9.00D)
        {
            stopWatch.Stop();
            MessageBox.Show("passed: " + hour.ToString("0.00"));
        }

那么,我在哪里写这段代码?如果你有更好的方法,请告诉我。

【问题讨论】:

标签: c# timer messagebox stopwatch


【解决方案1】:

除了使用其他人概述的 Timer 之外,您还可以直接使用 Stopwatch.Elapsed 返回的 TimeSpan 的 TotalHours() 属性:

        TimeSpan ts = stopWatch.Elapsed;
        if (ts.TotalHours >= 9)
        {
            MessageBox.Show("passed: " + ts.TotalHours.ToString("0.00"));
        }

【讨论】:

  • 您可以将它放在 Tick() 事件中,以代替之前手动计算“秒”、“分钟”和“小时”的代码。 *假设您没有使用韦斯顿的代码将 Interval() 设置为整个所需的持续时间! =)
【解决方案2】:

人们不明白的是,double hours 不太可能正好是 9.00!为什么不让你的计时器在你想要的时间之后触发一次,9 小时。

Timer timer;
public Form1()
{
    InitializeComponent();
    timer.Tick += timer_Tick;
    timer.Interval = TimeSpan.FromHours(9).TotalMilliseconds;
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    timer.Stop();
    MessageBox.Show("9 hours passed");
}

【讨论】:

    【解决方案3】:

    为了在特定时间段后执行特定任务,应使用 System.Forms.Timer(在 windows 窗体的情况下)。您可以使用它的 Elapsed 事件并在其中实现您的条件。

    【讨论】:

      【解决方案4】:

      尝试改用定时器。 Example from here

      Timer timer;
      public Form1()
      {
          InitializeComponent();
          timer.Tick += new EventHandler(timer_Tick); // when timer ticks, timer_Tick will be called
          timer.Interval = (1000) * (10);             // Timer will tick every 10 seconds
          timer.Enabled = true;                       // Enable the timer
          timer.Start();                              // Start the timer
      }
      
      void timer_Tick(object sender, EventArgs e)
      {
          double sec = stopWatch.ElapsedMilliseconds / 1000;
          double min = sec / 60;
          double hour = min / 60;
          if (hour == 9.00D)
          {
              stopWatch.Stop();
              MessageBox.Show("passed: " + hour.ToString("0.00"));
          }
      }
      

      【讨论】:

        【解决方案5】:

        使用Timer

        定时器定期调用代码。每隔几秒或几分钟,它 执行一个方法。这对于监控设备的健康状况很有用 重要的程序,如诊断。 System.Timers 命名空间 证明有用。

        看到这个:

         public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
        
                    stopWatch.Start();
                    tm.Interval = 1000;
                    tm.Enabled = true; 
                    tm.Tick += new EventHandler(tm_Tick);
                    tm.Start();
                }
        
                void tm_Tick(object sender, EventArgs e)
                {
                    double sec = stopWatch.ElapsedMilliseconds / 1000;
                    double min = sec / 60;
                    double hour = min / 60;
                    if (hour == 9.00D)
                    {
                        stopWatch.Stop();
                        MessageBox.Show("passed: " + hour.ToString("0.00"));
                    }
                }
             }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-21
          • 1970-01-01
          • 2013-12-09
          • 1970-01-01
          • 1970-01-01
          • 2021-03-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多