简体   繁体   English

单击按钮停止/启动计时器后,计时器停止工作

[英]TImer stops working after I click the button to stop/start it

Timer completely stops working it won't start again after I stop it how can I fix this?定时器完全停止工作,停止后它不会再次启动我该如何解决这个问题?

I am trying to have it toggle from 1 button first click toggles it if a grid is visible.我试图让它从 1 个按钮切换,如果网格可见,第一次单击切换它。

if the grid is not viable it will stop the timer/won't start the timer I am having the timer reference a different grid based on the same thing as above but for a different function.如果网格不可行,它将停止计时器/不会启动计时器我让计时器基于与上述相同的事物引用不同的网格,但针对不同的 function。

I'm pretty new to C# and wpf.我对 C# 和 wpf 很陌生。

If I need to give you guys more information for this just ask This code is in a different window from the mainwindow.如果我需要为此提供更多信息,请询问此代码位于与主窗口不同的 window 中。

The reference to the mainwindow is AS which I have a lot higher up in the code for this window.对主窗口的引用是 AS,我在这个 window 的代码中有很多。

This window is being referenced by the mainwindow to open/close.这个 window 被主窗口引用以打开/关闭。

private System.Windows.Threading.DispatcherTimer timer1;

public void InitTimer()
{
    timer1 = new System.Windows.Threading.DispatcherTimer();
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = TimeSpan.FromMilliseconds(2000); // in miliseconds
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    if (AS.Vis.Visibility != Visibility.Visible == false)
    {
        AS.Vis.Visibility = Visibility.Hidden;
    }
    else
    {
        AS.Vis.Visibility = Visibility.Visible;

        Fin();

        timer1.Start();
    }
}

private void Auto(object sender, RoutedEventArgs e)
{
    try
    {
        if (AS.tmr.Visibility != Visibility.Visible == false)
        {
            AS.tmr.Visibility = Visibility.Hidden;
            AKS.Foreground = new SolidColorBrush(Colors.Red);
            AKS.FontWeight = FontWeights.Bold;
            timer1.Stop();
        }
        else
        {
            AKS.Foreground = new SolidColorBrush(Colors.LimeGreen);
            AKS.FontWeight = FontWeights.Bold;
            AS.tmr.Visibility = Visibility.Visible;
            InitTimer();
            timer1.Start();
        }
    }
    catch (Exception)
    {
    }
}

Is the method Auto(object sender, RoutedEventArgs e) the only method who can start the timer (in the event timer1_Tick the timer is also started, but first if I understand your code snipped correct, the method Auto(object sender, RoutedEventArgs e) must call)?方法Auto(object sender, RoutedEventArgs e)是唯一可以启动计时器的方法吗(在事件timer1_Tick中,计时器也已启动,但首先如果我理解您的代码剪裁正确,方法Auto(object sender, RoutedEventArgs e)必须打电话)? When is this method called?什么时候调用这个方法? Maybe you can add this to the question.也许您可以将其添加到问题中。

If the method ( Auto(...) ) is called, can you check the if-condition .如果调用方法( Auto(...) ),您可以检查if-condition Maybe it will never go again into the else case.也许它永远不会 go 再次进入else案例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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