0

我正在制作一个程序,其中 timer1 应该激活另一个 timer2 然后停止,在 timer2 中我再次激活 timer1 并停止 timer2 等等它继续,然后我有一个文本日志,它写下进度。这就是问题所在,首先它从 Timer1 的 2 记号开始,然后是计时器 2 的 2,然后乘以 2,所以下一次是 4,然后是 8,然后是 16,依此类推,我只希望它是 1 timer1 而不是 1 timer2然后它重新开始,我看不出有什么问题。

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();

}
4

2 回答 2

2

timer1_tick您将事件添加到timer2.tick事件时,因此每次timer1_tick函数引发时,您都会再添加一个事件侦听器timer2,但永远不要删除旧的事件处理程序,与 timer2_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;

如果您这样做,您的计时器每次滴答将始终只调用一次函数。

于 2013-05-23T18:47:08.657 回答
1

我确定这就是@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") + ": "; }
    }

}
于 2013-05-23T20:23:14.973 回答