C# 如何在定时器中获得估计输出

C# 如何在定时器中获得估计输出,c#,timer,C#,Timer,我与两个计时器一起工作: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace example { public partial class Form1 :

我与两个计时器一起工作:

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

namespace example
{
    public partial class Form1 : Form
    {
        int i = 0;
        int j = 0;
        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 3000;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            i++;
            timer2.Enabled = true;
            if (i < 3)
                time1(i);
            else
                timer1.Enabled = false;


        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            j++;
            timer2.Interval = timer1.Interval / 5;
            if (j < 5)
                time2(j);
            else
                timer2.Enabled = false;

        }

        private void time1(int i)
        {

            MessageBox.Show(i.ToString(), "First Timer");
        }

        private void time2(int j)
        {
            MessageBox.Show(j.ToString(), "SecondTimer");
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间示例
{
公共部分类Form1:Form
{
int i=0;
int j=0;
公共表格1()
{
初始化组件();
计时器1.间隔=3000;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
timer1.Enabled=true;
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
i++;
timer2.Enabled=true;
如果(i<3)
时间1(i);
其他的
timer1.Enabled=false;
}
私有无效计时器2_刻度(对象发送方,事件参数e)
{
j++;
timer2.Interval=timer1.Interval/5;
if(j<5)
时间2(j);
其他的
timer2.Enabled=false;
}
专用无效时间1(int i)
{
Show(i.ToString(),“First Timer”);
}
专用无效时间2(int j)
{
Show(j.ToString(),“SecondTimer”);
}
}
}
运行此程序时,会产生如下输出:

第一个计时器:1
第二个计时器:1
第二个计时器:2
第二个计时器:3
第二个计时器:4
第一次:2

在消息框中

但在调试时,调试不能按该顺序移动。完成第二个计时器后:2返回第一个计时器。但是我需要按照相同的顺序进行调试,就像没有断点一样。

我需要在另一个应用程序中使用它。为什么会发生这种情况?

可能是因为您正在调试

尝试将时间间隔乘以10,这样您就可以手动密切跟踪代码,因为它不需要单步执行


糟糕的英语顺便说一句。我甚至不知道我在这里是否正确理解了您。

当您调试应用程序时,您不能依靠计时器以正确的间隔启动,因为程序可能会在断点处停止。尝试删除所有断点,并查看计时器在调试时是否工作。

问题是,即使您使用断点停止了程序,底层计时器仍将继续执行。您正在使用的
System.Windows.Forms.Timer
将触发另一个事件(而
System.Timers.Timer
将继续触发许多事件)。您可以自己尝试:

将计时器设置为3000ms的间隔,并在其eventhandler中设置断点:

private void timer1_Tick( object sender, EventArgs e )
{
    // insert breakpoint here
}
等待约3秒钟,然后继续您的程序。将立即再次调用eventhandler。下一次等待时间更长(~10秒),结果相同-eventhandler将立即触发一次,然后在~3秒后触发。将行为与
System.Timers.Timer
进行比较,后者将在第二个测试用例中多次触发事件

因此,根据您的断点(是否在
timer1\u勾选
before
timer2.Enabled=true;
或after?)以及在继续执行之前停止程序的时间,您将得到不同的结果

不幸的是,您无能为力。在您的特殊情况下,您可以在设置断点之前停止所有计时器,如:

private static void Break()
{
    var timer1Enabled = timer1.Enabled;
    var timer2Enabled = timer2.Enabled;
    timer1.Stop();
    timer2.Stop();
    // insert breakpoint here
    timer1.Enabled = timer1Enabled;
    timer2.Enabled = timer2Enabled;
}
但这将重新启动计时器,并且计时器1和计时器2之间的间隔比例将是错误的


有关.net计时器的更多信息,您可以阅读。

我不知道在调试时计时器是否在等待您时停止计数……我确实认为计时器正在运行,即使在调试中也是如此。