简体   繁体   中英

Timer counting in Win8 app

I am building a win8 app which has 3 buttons(Button1, Button2 and StartButton) and timer in it. Button1 and Button2 are disabled. If clicked on StartButton, Button1 is enabled and the number of clicks within 20 secs is counted and displayed in a textblock1. After the timer is over Button1 is disabled and Button2 is enabled and clicks are counted and displayed in textblock2. My problem is that the timer ticks properly for Button1 and not for Button2. Timer becomes faster when button2 is enabled. Can someone help me? My code is below:

    private int count1=0;
    private int count2=0;
    private int clickCounter = 0;
    private int timeLeft;
    private DispatcherTimer timer;

    private void StartTimer()
    {
        if (this.timer != null)
        {
            this.StopTimer();
        }
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0,0,0,1);
        timer.Tick += timer_Tick;
        timer.Start();

    }

    private void StopTimer()
    {
        if (this.timer != null)
        {
            this.timer.Stop();
            this.timer = null;
        }
    }

    public void timer_Tick(object sender, object args)
    {

        if (timeLeft > 0)
        {
            timeLeft = timeLeft - 1;
            timerTextBlock.Text = Convert.ToString(timeLeft);
            this.StartButton.IsEnabled = false;
        }
        else
        {
            StopTimer();
            if (clickCounter==2)
            {
                ShowResult();
                this.Button2.IsEnabled = false;
                this.StartButton.IsEnabled = false;
            }
            else
            {
                myMsg.Text = "Time's up!";
                this.Button1.IsEnabled = false;
                this.StartButton.IsEnabled = true;
            }
        }
    }

    private void Button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        // TODO: Add event handler implementation here.
        count1++;
        this.textblock1.Text=count1.ToString();

    }

    private void Button2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        count2++;
        this.textblock2.Text=count2.ToString();
    }
    public void ResetTimer()
    {
            timeLeft = 20;
    }
    private void StartButton_Click(object sender, RoutedEventArgs e)
    {
        clickCounter++;
        if (textblock1.Text == "0")
        {
            ResetTimer();
            StartTimer();
            this.Button1.IsEnabled = true;
        }
        else
        {
            ResetTimer();
            StartTimer();
            this.Button2.IsEnabled = true;
        }
    }

Every time you call the StartTimer Method timer.Tick += timer_Tick; is executed. This means if you call StartTimer the second time, every tick will invoke two events. Split your code like:

private void InitTimer()
{
    timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0,0,0,1);
    timer.Tick += timer_Tick;
}

private void StartTimer()
{
    timer.Start();
}

and call InitTimer only ones. Another Option is to unregister your event in if (this.timer != null) with the code timer.Tick -= timer_Tick; A second point I see is a naming conflict: You've got a private global variable timer and one variable timer in your StartTimer Method.

// Edit: Full updated code:

private int count1=0;
private int count2=0;
private int clickCounter = 0;
private int timeLeft;
private DispatcherTimer timer;

private void StartTimer() {
    if (timer == null) {
        timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0,0,0,1);
        timer.Tick += timer_Tick;
    }
    timer.Stop();
    timeLeft = 20;
    timer.Start();
}

public void timer_Tick(object sender, object args) {
    if (timeLeft > 0) {
        timeLeft = timeLeft - 1;
        timerTextBlock.Text = Convert.ToString(timeLeft);
    } else {
        timer.Stop();
        if (clickCounter==2) {
            ShowResult();
            Button2.IsEnabled = false;
            StartButton.IsEnabled = false;
        } else {
            myMsg.Text = "Time's up!";
            Button1.IsEnabled = false;
            StartButton.IsEnabled = true;
        }
    }
}

private void Button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
    count1++;
    textblock1.Text=count1.ToString();
}

private void Button2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
    count2++;
    textblock2.Text=count2.ToString();
}

private void StartButton_Click(object sender, RoutedEventArgs e) {
    clickCounter++;
    StartButton.IsEnabled = false;
    if (textblock1.Text == "0"){
        Button1.IsEnabled = true;
        StartTimer();
    } else {
        Button2.IsEnabled = true;
        StartTimer();
    }
}

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