0

I'm stuck here. I have a checkbox. When checked, start timer. When unchecked stop timer. I can use some help here. I have tried checked, unchecked, and click events. Nothing is stopping the timer. It just keeps running...

Xaml: (has all three events just for show)

<CheckBox x:Name="CbAutoRefresh" Grid.Row="1" ClipToBounds="True" HorizontalAlignment="Left" Content="Enable Auto Refresh" Margin="10,0,0,0" Width="150" Click="CbAutoRefresh_Click" Checked="CbAutoRefresh_Checked" Unchecked="CbAutoRefresh_Unchecked" />

C#: (all three attempts)

private void CbAutoRefresh_Click(object sender, RoutedEventArgs e)
{

    var aTimer = new Timer();
    if (CbAutoRefresh.IsChecked == true)
    {
        //start a timer:

        aTimer.Elapsed += OnTimedEvent;
        aTimer.Interval = 60000;
        aTimer.Enabled = true;
    }
    else
    {
        aTimer.Enabled = false;
    }
}

private void CbAutoRefresh_Checked(object sender, RoutedEventArgs e)
{
    //start a timer:
    var aTimer = new Timer();
    aTimer.Elapsed += OnTimedEvent;
    aTimer.Interval = 60000;
    aTimer.Enabled = true;
}

private void CbAutoRefresh_Unchecked(object sender, RoutedEventArgs e)
{
    var aTimer = new Timer {Enabled = false};
}

I even tried this, which was mention at Stack Overflow

<CheckBox Checked="CheckBoxChanged" Unchecked="CheckBoxChanged"/>

private void CheckBoxChanged(object sender, RoutedEventArgs e)
{
  MessageBox.Show("Eureka, it changed!");
}
RazorSharp
  • 179
  • 1
  • 3
  • 15
  • 2
    don't create a `new Timer` on every event. Declare 1 timer in Window object (window field/property), initialize it in constructor and work with it in any event handlers. At the moment you are running *multiple* timers – ASh Jul 25 '17 at 18:27

2 Answers2

1

Remove the statement

var aTimer = new Timer(); 

from withing the clicked handler. move it to the constructor or some function run just once during the object's lifetime. Use Timer's Start() & Stop() methods to start and stop the timer.

1

don't create a new Timer on every event. Declare 1 timer in Window object (window field/property), initialize it in constructor and work with it in any event handlers. At the moment you are running multiple timers

<CheckBox x:Name="CbAutoRefresh" Checked="CheckBoxChanged" Unchecked="CheckBoxChanged"/>
public class MyWindow()
{
   private Timer _t;
   public MyWindow()
   {
      InitializeComponent();
      _t = new Timer();
      _t.Elapsed += OnTimedEvent;
      _t.Interval = 60000;
   }

   private void CheckBoxChanged(object sender, RoutedEventArgs e)
   {
      _t.Enabled = CbAutoRefresh.IsChecked;
   }    
}
ASh
  • 34,632
  • 9
  • 60
  • 82
  • Very minor tweak, but exactly what I was looking for. Thank you ||| if (CbAutoRefresh.IsChecked != null) _t.Enabled = (bool) CbAutoRefresh.IsChecked; – RazorSharp Jul 25 '17 at 18:58
  • @dot3tech, I really forgot IsChecked is nullable. consider using `_t.Enabled = CbAutoRefresh.IsChecked ?? false` (treat `null` as `false` too, code is shorter) – ASh Jul 25 '17 at 19:07