1

DispatcherTimer is not executing code when I add conditions or try and start the timer outside the constructor.

The problem I am having is if I start the DispatchTimer in the constructor without condition my graphics layer adds before button Show_ClickStuff is clicked. If I don't start everything in the constructor and I place my if conditions in the Show_ClickStuff or in _timer_tick I can place Break Points on the code and watch the timer hit the lines of code but will not execute the lines of code. If I remove conditions code will execute. I have tried all kinds of variations and can not get this to work the way I need

using System.Windows.Threading; .NET Framework 4.5 Code file: .xaml.cs Silverlight 4

public Tool()
{
     InitializeComponent();

      _timer1 = new DispatcherTimer();
      //_timer1.Start();
      _timer1.Interval = TimeSpan.FromMilliseconds(4000);
      _timer1.Tick += new EventHandler(_timer_tick);
}

private void Show_ClickStuff(object sender, RoutedEventArgs e)
        {
            ToggleStuff();
            if (showStuff.IsChecked == true)
            {
                //Timer_Toggle(this, null);
                //_timer1 = new DispatcherTimer();
                //_timer1.Start();
                //_timer1.Interval = TimeSpan.FromMilliseconds(4000);
                //_timer1.Tick += new EventHandler(_timer_tick);
                _timer1.Start();
            }
            else if (!_timer1.IsEnabled && showStuff.IsChecked == true)
            {
                _timer1.Start();  
            }
            else
            {
                _timer1.Stop();
            }

        }

private void _timer_tick(object sender, EventArgs e)
        {
                _map.Layers.Remove(_stuffLayer);
                GetStuff();
                _map.Layers.Add(_stuffLayer);
        }
Neodev
  • 11
  • 1

0 Answers0