Reputation: 43
I am making a program where the timer1 should activate another timer2 and then stop, in timer2 i activate timer1 again and stop timer2 and so it goes on and then I have a text log where it write the progress down. And here is the problem, first its starts with 2 of Timer1 tick written out then 2 of timer 2 then it gets multiplied by 2 so its 4 next time then 8 then 16 so forth and I just want it to be 1 timer1 than 1 timer2 then its starts over again, I can't see what's wrong.
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();
}
Upvotes: 0
Views: 2717
Reputation: 39122
I'm sure this is what @Epsil0neR means...
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") + ": "; }
}
}
Upvotes: 1
Reputation: 1704
on timer1_tick
you add event to timer2.tick
event, so every time when timer1_tick
function raises, you add one more event listener to timer2
, but never remove old event handlers, the same situation with timer2_tick.
My advice to you is add these lines to your constructor and remove these lines from other functions:
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = (1000);
timer1.Enabled = true;
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = (1000);
timer2.Enabled = true;
If you will do that, your timers will call always only once function per tick.
Upvotes: 2