简体   繁体   中英

more than one, independent dispatchertimer?

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?

Sry misread the code. Will have another look.

Could you post the code for "string2time" and "time2string"?

Also check if your actual code is the same as your posted code:

private DispatcherTimer d1, blueTimer;    // <-- d1 and blueTimer? blueTimer is never 
                                          //mentioned. However a "redTimer" object appears in your code

private void but1_Click(object sender, RoutedEventArgs e)
{
    if (redTimer == null)   // <-- shouldn´t this be "(d1 == 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();
    }
}

I am not sure about what the problem is. As far as I get it, you have 2 textBoxes in which you enter the time. In your example the first on reads "15" and the second "10". Now you click the first button, and the "d1" starts counting down the time in the first box. When it reached "10", as in the same as the other one, you click the second button. Which starts "d2" who will then count down the text in the second box at the same interval. If that is what´s happening, then the code is doing what it´s supposed to do? Or what effect were you trying to achieve. Not sure.

Please clarify the problem.

EDIT:

You could simplify this:

private string time2string(int time) 
{
    string stime = "";
    int m, s = 0;

    m = time / 60;
    //s = time - m * 60;
    s = time % 60;  // this is a modulo, it will divide the number by 60 and provide the 
                    // rest. Learn to use it, it may save you a lot of work sometimes.

    //stime = m < 10 ? "0" + m.ToString() : m.ToString();
    //stime += ":" + (s < 10 ? "0" + s.ToString() : s.ToString());
    stime = string.Format("{0:D2}:{1:D2}", m, s};    // does the same as the two previous lines
    return stime;
}

Apart from that I also don´t see where your code is going wrong. You could maybe check if one or both of the textBoxes have a "xyz_Changed" Event, that´s being handled in such a way that both display the same value. Maybe you used this for ease of getting both to display the same start value or something.

The only other thing I can suggest is that you could post the complete code you have so far.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM