【问题标题】:Why Doesnt this Simple Modification (to working code) for Making a 5 second timer work?为什么这个用于制作 5 秒计时器的简单修改(对工作代码)不起作用?
【发布时间】:2018-01-05 05:14:55
【问题描述】:

我有一个简单的 C# 程序(我使用的是 Visual Studio 2010)-只需一个表单上的按钮-如下面最底部的链接所示- 当按下此按钮时,它会在消息框中显示每秒经过的时间。

这在这个程序中没有错误,它在下面给出并且完全工作。 它的工作原理是在按下按钮后将变量 starttodisplaytimeronscreen 设置为 true - 这是一个标志 - 并且一旦设置为 true 它可以显示每秒显示的消息框。 这是工作代码(我正在使用的完整程序)

using System;
using System.Timers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace finitetimerprogram
{
public partial class Form1 : Form
{
 int thetimeinsecs = 0;// is the number of seconds as an integer that have elapsed since button pressed
  bool starttodisplaytimeronscreen = false;// this is a flag that set to true if button is pressed
  System.Timers.Timer mytimer = new System.Timers.Timer();

  private void customfn(object source, ElapsedEventArgs e)
        {
      if (starttodisplaytimeronscreen == true)
   {  // starttodisplaytimeronscreen becomes true if the button is clicked
                thetimeinsecs = thetimeinsecs + 1;
  MessageBox.Show("seconds since  pressed= " + thetimeinsecs.ToString(), "seconds since pressed= " + thetimeinsecs.ToString());
            }
        }

        public Form1()
        {  // this function is called continually from since when the form is shown onscreen
            InitializeComponent();
            mytimer.Elapsed += new ElapsedEventHandler(customfn);
            mytimer.Interval = 1000;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            starttodisplaytimeronscreen = true;
            mytimer.Start();
        }


    }
}

现在我想修改这个程序,让它在 5 秒后停止显示计时器- 换句话说,单击按钮会激活一个 5 秒计时器,该计时器显示为 在屏幕上显示为 1、2、3、4 和 5 秒的五个消息框。 然后,如果用户再次(再一次)按下按钮,则一旦计时器停止,它将再次启动 5 秒计时器(假设在这种情况下,每次用户想要创建 5 秒计时器时,仅单击一次按钮)。

我已经通过添加简单的代码来做到这一点

  if (thetimeinsecs > 5)
 {// this if statement stop the timer after 5 seconds but doesnt work
  starttodisplaytimeronscreen = false;// supposed to stop displaying the message box after 5 secods
  thetimeinsecs = 0;
   mytimer.Stop();
        }

在 5 秒后重置计时器,将标志 starttodisplaytimeronscreen 重置为 false。将 timeinsecs 重置为零,并使用 mytimer.Stop() 停止计时器。 我遇到的问题是这样一个简单的代码来修改工作代码(上面最初给出)以使 5 秒计时器不起作用。所以我的问题是为什么它不起作用? - 有什么简单的方法来解决它?

下面是完整的非工作代码 - 只是为了清楚起见。

using System;
using System.Timers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace finitetimerprogram
{
    public partial class Form1 : Form
    {
       int thetimeinsecs = 0;// is the number of seconds as an integer that have elapsed since button pressed
        bool starttodisplaytimeronscreen = false;// this is a flag that set to true if button is pressed
        System.Timers.Timer mytimer = new System.Timers.Timer();

        private void customfn(object source, ElapsedEventArgs e)
        {
            if (starttodisplaytimeronscreen == true)
            {  // starttodisplaytimeronscreen becomes true if the button is clicked
                thetimeinsecs = thetimeinsecs + 1;
                MessageBox.Show("seconds since  pressed= " + thetimeinsecs.ToString(), "seconds since pressed= " + thetimeinsecs.ToString());   
            }


            if (thetimeinsecs > 5)
            {// this if statment stop the timer after 5 seconds but doesnt work
                starttodisplaytimeronscreen = false;// supposed to stop displaying the message box after 5 secods
                thetimeinsecs = 0;
                mytimer.Stop();
            }

        }

        public Form1()
        {  // this function is called continually from since when the form is shown onscreen
            InitializeComponent();
            mytimer.Elapsed += new ElapsedEventHandler(customfn);
            mytimer.Interval = 1000;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            starttodisplaytimeronscreen = true;
            mytimer.Start();
        }

shows what happens after 6 seconds since the button was pressed

【问题讨论】:

  • 你不必做这么多的实验。不要每次都启动/停止您的mytimer.Start() .. 启动您的计时器一次。你最好只更改myTimer.Interval = anyvalue
  • 乍一看,您的两个代码示例看起来相同,并且都包含您声称要添加的代码部分。当然,代码很难阅读,因为你放了太多的空白,一次很难看到很多。请将您的代码简化为一个单个示例,以显示您尝试过的内容,准确而简洁地解释代码的作用以及您希望它做什么。
  • @PeterDuniho 我一定是不小心将相同的代码复制并粘贴到了问题中。所以我修好了。第一部分给出了有效的未修改代码。第二部分是修改后的代码。我认为有两个程序更清楚 - 一个有效,一个修改
  • 我不同意。而且您仍然没有删除多余的空白行。但是无所谓。更重要的是,您是否在每次显示消息框时都将其关闭?因为如果没有,你说的代码就不会执行。
  • @PeterDuniho 我之前没有关闭消息框,但如果消息框被及时关闭,那么我的代码每秒都可以工作,所以问题的答案与此有关。问题中给出的修改后的代码确实会运行并显示消息框 - 但如果消息框没有关闭,它会显示 5 秒后的秒数

标签: c# timer


【解决方案1】:

简单的解决方法是如下所示交换你的函数行:

private void customfn(object source, ElapsedEventArgs e)
{
    if (thetimeinsecs >= 5)
    {// this if statment stop the timer after 5 seconds but doesnt work
        starttodisplaytimeronscreen = false;// supposed to stop displaying the message box after 5 secods
        thetimeinsecs = 0;
        mytimer.Stop();
    }

    if (starttodisplaytimeronscreen == true)
    {  // starttodisplaytimeronscreen becomes true if the button is clicked
        thetimeinsecs = thetimeinsecs + 1;
        MessageBox.Show("seconds since  pressed= " + thetimeinsecs.ToString(), "seconds since pressed= " + thetimeinsecs.ToString());
    }

}

如果你想进一步改进它,你可以通过稍微改变逻辑来摆脱starttodisplaytimeronscreen。我会把它留给你。

【讨论】:

  • 我试过了,这不是答案 - 所以你的答案不正确。
  • 从上面的评论开始,它与在消息框出现时立即关闭消息框有关 - 然后代码工作 - 但我不想每秒钟都关闭它 - 只是希望它停止显示消息自动框,无需用户解雇
  • 问题仍未得到解答
  • @user43655672 我正在用修改后的代码运行程序。它显示 5 个消息框和时间停止。再次单击按钮,它将在计时器停止之前再显示 5 个消息框。无需清除消息框。请使用修改后的代码。
  • 如果您的答案是正确的,我感到困惑的是您只是将条件 thetimeinsecs >= 5 从 thetimeinsecs >5 更改(这是我第一次看到它时的条件,而这个旧条件使它上升到 6 秒——所以我错误地认为它会继续运行)。所以你的答案的新版本是正确的。
猜你喜欢
  • 2013-05-04
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多