无法正确重新启动DispatcherTimer

时间:2015-07-31 06:17:03

标签: c# wpf dispatchertimer

我正在尝试使用三个按钮在我的应用程序中实现一个计时器 - 启动,停止和重启。

计时器启动并停止正常。但是,当我点击重启按钮时,它从之前的秒和分钟值开始,而不是从0和0开始。我无法看到我做错了,因为我在开始时将秒和分钟设置为0重启方法本身。

例如:第一次从0开始并继续到1,2,3。停止在3.当我重新启动时,它从2开始并继续到4,6 ...如果我再次重新启动它从3开始并继续到6,9,12 ......

以下是我的代码:

    public int seconds;
    public int minutes;
    public int hours;

    System.Windows.Threading.DispatcherTimer myTimer = new System.Windows.Threading.DispatcherTimer();

    private void Tick_Timer_Start(object sender, RoutedEventArgs e)
    {
        myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        myTimer.Tick += new EventHandler(Each_Tick);
        myTimer.Start();
    }

    private void Tick_Timer_Stop(object sender, RoutedEventArgs e)
    {
        myTimer.Stop();
    }

    private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
    {
        seconds = 0;
        minutes = 0;
        Timertext.Text = "0 minutes : 0 seconds";
        myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        myTimer.Tick += new EventHandler(Each_Tick);
        myTimer.Start();

    }

    private void Each_Tick(object o, EventArgs sender)
    {
        seconds = seconds + 1;

        if (seconds < 10 && minutes < 10)
        {
            Timertext.Text =  minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
        }
        else if (seconds > 59)
        {
            minutes = minutes + 1;
            seconds = 0;
            seconds = seconds + 1;
            Timertext.Text =  minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
        }
        else
        {
            Timertext.Text =  minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
        }
    }

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:3)

您的计划永远不会按预期工作! 每次单击开始按钮时,都会向Dispatchertimer添加Tickhandler,以便秒开始增加2然后增加3,依此类推。 您必须在Stop和Restart方法中删除Handler! 然后所有都按预期工作,您不必在每次重新启动时创建新的DispatcherTimer。顺便说一下,@ DeshDeep Singh的解决方案永远不会收集旧的DispatcherTimer,因为EventHandler不会收集它 - 因此他的解决方案会给你带来很大的内存问题。

以下是工作代码:

public int seconds;
public int minutes;
public int hours;

System.Windows.Threading.DispatcherTimer myTimer = new System.Windows.Threading.DispatcherTimer();

private void Tick_Timer_Start(object sender, RoutedEventArgs e)
{
    myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
    myTimer.Tick += Each_Tick;
    myTimer.Start();
}

private void Tick_Timer_Stop(object sender, RoutedEventArgs e)
{
    myTimer.Tick -= Each_Tick;
    myTimer.Stop();
}

private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
{
    myTimer.Tick -= Each_Tick;
    seconds = 0;
    minutes = 0;
    Timertext.Text = "0 minutes : 0 seconds";
    myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
    myTimer.Tick += new EventHandler(Each_Tick);
    myTimer.Start();
}

private void Each_Tick(object sender, EventArgs e)
{
    seconds = seconds + 1;
    if (seconds < 10 && minutes < 10)
    {
        Timertext.Text = minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
    }
    else if (seconds > 59)
    {
        minutes = minutes + 1;
        seconds = 0;
        seconds = seconds + 1;
        Timertext.Text = minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
    }
    else
    {
        Timertext.Text = minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
    }
}

答案 1 :(得分:2)

您需要做的就是仅将Tick处理程序附加

private int seconds;
private int minutes;
private int hours;
private DispatcherTimer myTimer = new DispatcherTimer();

private void Initialize()
{
    myTimer.Interval = TimeSpan.FromMilliseconds(1000);
    myTimer.Tick += Each_Tick;
}

private void Tick_Timer_Start(object sender, RoutedEventArgs e)
{
    myTimer.Start();
}

private void Tick_Timer_Stop(object sender, RoutedEventArgs e)
{
    myTimer.Stop();
}

private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
{
    myTimer.Stop();
    seconds = 0;
    minutes = 0;
    Timertext.Text = "0 minutes : 0 seconds";
    myTimer.Start();
}

private void Each_Tick(object o, EventArgs sender)
{
    ...
}

更简单的解决方案是只有Start和Stop,而不需要重启:

private int seconds;
private int minutes;
private int hours;
private DispatcherTimer myTimer = new DispatcherTimer();

private void Initialize()
{
    myTimer.Interval = TimeSpan.FromMilliseconds(1000);
    myTimer.Tick += Each_Tick;
}

private void Tick_Timer_Start(object sender, RoutedEventArgs e)
{
    myTimer.Stop();
    seconds = 0;
    minutes = 0;
    Timertext.Text = "0 minutes : 0 seconds";
    myTimer.Start();
}

private void Tick_Timer_Stop(object sender, RoutedEventArgs e)
{
    myTimer.Stop();
}

private void Each_Tick(object o, EventArgs sender)
{
    ...
}

答案 2 :(得分:1)

由于计时器未重置,请执行以下操作:

private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
    {
        myTimer = new System.Windows.Threading.DispatcherTimer();

        seconds = 0;
        minutes = 0;
        Timertext.Text = "0 minutes : 0 seconds";
        myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        myTimer.Tick += new EventHandler(Each_Tick);
        myTimer.Start();

    }

答案 3 :(得分:0)

     // put   myTimer.Stop() in Restart Method

   private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
    {
        myTimer.Stop();  // stop timmer here and try
        seconds = 0;
        minutes = 0;
        Timertext.Text = "0 minutes : 0 seconds";
        myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        myTimer.Tick += new EventHandler(Each_Tick);
        myTimer.Start();

    }