Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计时器一次滴答不止一次_C#_Timer - Fatal编程技术网

C# 计时器一次滴答不止一次

C# 计时器一次滴答不止一次,c#,timer,C#,Timer,我正在制作一个程序,计时器1应该激活另一个计时器2,然后停止,在计时器2中,我再次激活计时器1,然后停止计时器2,然后它继续运行,然后我有一个文本日志,其中记录了进度。这就是问题所在,首先它以2个计时器1的刻度开始,然后是2个计时器2,然后它乘以2,所以下次是4,然后是8,然后是16,如此类推,我只想它是1个计时器1,而不是1个计时器2,然后它重新开始,我看不出有什么不对 private void buttonStart_Click(object sender, EventArgs e) {

我正在制作一个程序,计时器1应该激活另一个计时器2,然后停止,在计时器2中,我再次激活计时器1,然后停止计时器2,然后它继续运行,然后我有一个文本日志,其中记录了进度。这就是问题所在,首先它以2个计时器1的刻度开始,然后是2个计时器2,然后它乘以2,所以下次是4,然后是8,然后是16,如此类推,我只想它是1个计时器1,而不是1个计时器2,然后它重新开始,我看不出有什么不对

private void buttonStart_Click(object sender, EventArgs e)
{
    buttonStart.Enabled = false;
    buttonStop.Enabled = true;

    timer1.Tick += new EventHandler(timer1_Tick); 
    timer1.Interval = (1000);             
    timer1.Enabled = true;                       
    timer1.Start();

}

private void buttonStop_Click(object sender, EventArgs e)
{
    buttonStart.Enabled = true;
    buttonStop.Enabled = false;

    timer1.Stop();
    timer2.Stop();
}

private void LogWrite(string txt)
{
    textBoxCombatLog.AppendText(txt + Environment.NewLine);
    textBoxCombatLog.SelectionStart = textBoxCombatLog.Text.Length;
}

private void timer1_Tick(object sender, EventArgs e)
{
    LogWrite(TimeDate + "player hit");

    timer1.Stop();

    timer2.Tick += new EventHandler(timer2_Tick);
    timer2.Interval = (1000);
    timer2.Enabled = true;
    timer2.Start();


}

private void timer2_Tick(object sender, EventArgs e)
{
    LogWrite(TimeDate + "mob hit");

    timer2.Stop();

    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = (1000);
    timer1.Enabled = true;
    timer1.Start();

}

timer1\u tick
上,您将事件添加到
timer2.tick
事件中,因此每当
timer1\u tick
函数启动时,您都会向
timer2
添加一个事件侦听器,但决不会删除旧的事件处理程序,与timer2\u tick的情况相同

我的建议是将这些行添加到构造函数中,并从其他函数中删除这些行:

timer1.Tick += new EventHandler(timer1_Tick); 
timer1.Interval = (1000);             
timer1.Enabled = true;   

timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = (1000);
timer2.Enabled = true;

如果您这样做,您的计时器将始终只在每个刻度上调用一个函数。

timer1\u tick
上,您将事件添加到
timer2.tick
事件中,因此每次
timer1\u tick
函数启动时,您都会向
timer2
添加一个事件侦听器,但永远不要删除旧的事件处理程序,计时器2_tick的情况也一样

我的建议是将这些行添加到构造函数中,并从其他函数中删除这些行:

timer1.Tick += new EventHandler(timer1_Tick); 
timer1.Interval = (1000);             
timer1.Enabled = true;   

timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = (1000);
timer2.Enabled = true;

如果您愿意这样做,您的计时器将始终只在每个刻度上调用一次函数。

我相信这就是@Epsil0neR的意思

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = (1000);
        timer1.Enabled = false;

        timer2.Tick += new EventHandler(timer2_Tick);
        timer2.Interval = (1000);
        timer2.Enabled = false;
    }

    private void buttonStart_Click(object sender, EventArgs e)
    {
        buttonStart.Enabled = false;
        buttonStop.Enabled = true;

        timer1.Start();
    }

    private void buttonStop_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        timer2.Stop();

        buttonStart.Enabled = true;
        buttonStop.Enabled = false;
    }

    private void LogWrite(string txt)
    {
        textBoxCombatLog.AppendText(txt + Environment.NewLine);
        textBoxCombatLog.SelectionStart = textBoxCombatLog.Text.Length;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();

        LogWrite(TimeDate + "player hit");

        timer2.Start();
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        timer2.Stop();

        LogWrite(TimeDate + "mob hit");

        timer1.Start();
    }

    private string TimeDate
    {
        get { return DateTime.Now.ToString("HH:mm:ss") + ": "; }
    }

}

我相信这就是@Epsil0neR的意思

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = (1000);
        timer1.Enabled = false;

        timer2.Tick += new EventHandler(timer2_Tick);
        timer2.Interval = (1000);
        timer2.Enabled = false;
    }

    private void buttonStart_Click(object sender, EventArgs e)
    {
        buttonStart.Enabled = false;
        buttonStop.Enabled = true;

        timer1.Start();
    }

    private void buttonStop_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        timer2.Stop();

        buttonStart.Enabled = true;
        buttonStop.Enabled = false;
    }

    private void LogWrite(string txt)
    {
        textBoxCombatLog.AppendText(txt + Environment.NewLine);
        textBoxCombatLog.SelectionStart = textBoxCombatLog.Text.Length;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();

        LogWrite(TimeDate + "player hit");

        timer2.Start();
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        timer2.Stop();

        LogWrite(TimeDate + "mob hit");

        timer1.Start();
    }

    private string TimeDate
    {
        get { return DateTime.Now.ToString("HH:mm:ss") + ": "; }
    }

}

您应该只执行以下几行:timer2.Tick+=neweventhandler(timer2\u Tick);一次且仅一次(因此,将其从按钮中删除)点击表单的加载/初始化。您可能需要查看一个类似问题的答案,该问题涉及为什么传统计时器通常不用于游戏中。如果间隔()因为两者总是相同的,那么只需使用一个计时器并切换一个布尔值来指示当前正在攻击的人……您应该只执行以下几行操作:timer2.Tick+=neweventhandler(timer2\u Tick);一次且仅一次(因此,请从按钮中删除它们,然后单击表单的加载/初始化。您可能需要查看一个类似问题的答案,该问题涉及为什么游戏中通常不使用传统计时器。如果间隔()因为两者都是相同的,那么只需使用一个计时器并切换一个布尔值来指示当前正在攻击的人…它确实起到了一定的作用,但仍然存在一个问题tho,它仍然会一直写2行而不是1行,我这次将Eventhandler放在开始按钮中。不明白为什么会这样。因为你有地方在
timer1.tick
timer2.tick
上为函数
timer1.tick
添加了侦听器,因此请检查您的代码并确保每个函数只有一个事件侦听器。“侦听器”是什么意思?
timer1.tick+=新事件处理程序(timer1.tick)
我的意思是,事件监听器是
timer1\u tick
。对不起,我的英语不好,但我认为事件监听器和事件处理程序在编程中是一样的。你的意思是我有timer1。tick+=新的事件处理程序(timer1\u tick);在计时器内1.勾选表格?我不应该这样做?因为如果这是重点,Epsli0neR已经告诉我该怎么做。它做了一些工作,但仍然有一个问题,它仍然一直写2行而不是1行,我这次把Eventhandler放在开始按钮中。不明白为什么它会这样。因为你在
timer1.tick
timer2.tick
上为函数
timer1\u tick
添加了侦听器,因此请检查代码并确保每个函数只有一个事件侦听器。“侦听器”是什么意思?
timer1.tick+=新事件处理程序(timer1\u tick)
我的意思是,事件监听器是
timer1\u tick
。对不起,我的英语不好,但我认为事件监听器和事件处理程序在编程中是一样的。你的意思是我有timer1。tick+=新的事件处理程序(timer1\u tick);在计时器1内。勾选表格?我不应该这样做?因为如果这是重点,Epsli0neR已经告诉我该怎么做。我明白了,但这仍然没有改变问题,它连续键入“mob hit”两次和“player hit”我可以发布一个屏幕截图,描述我的意思。很可能你已经通过IDE连接了两个计时器,不需要通过代码。删除以下行:
timer1.Tick+=neweventhandler(timer1_Tick);
timer2.Tick+=neweventhandler(timer2_Tick);
我明白了,但这仍然没有改变问题,它连续两次输入“mob hit”和“player hit”连续两次,而不是一次,我可以发布一个屏幕截图,描述一下我的意思很可能你已经通过IDE连接了两个计时器,不需要通过代码。删除以下行:
timer1.Tick+=neweventhandler(timer1_Tick);
timer2.Tick+=neweventhandler(timer2_Tick);