I want to create few timers, counting down x time, working independly, updating time in textBlock, and doing something when finished.
So I wrote:
private DispatcherTimer d1, blueTimer;
private void but1_Click(object sender, RoutedEventArgs e)
{
if (redTimer == null)
{
d1 = new System.Windows.Threading.DispatcherTimer();
d1.Tick += new EventHandler(d1_Tick);
d1.Interval = new TimeSpan(0, 0, 1);
d1.Start();
}
}
private void but2_Click(object sender, RoutedEventArgs e)
{
if (d2 == null)
{
d2 = new System.Windows.Threading.DispatcherTimer();
d2.Tick += new EventHandler(d2_Tick);
d2.Interval = new TimeSpan(0, 0, 1);
d2.Start();
}
}
private void d1_Tick(object sender, EventArgs e)
{
int time = string2time(t1.Text);
if (time > 0)
{
t1.Text = time2string(--time);
}
else
{
d1.Stop();
}
}
private void d2_Tick(object sender, EventArgs e)
{
int time = string2time(t2.Text);
if (time > 0)
{
t2.Text = time2string(--time);
}
else
{
d2.Stop();
}
}
Time is for example 15 seconds. When I click but1, time is counting down, when t1 is 10 sec I click but2, t2 is 10 sec too, and its counting down having the same time.
Why does it happen? How to avoid that?