Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we set Timers in WinRT app?

I am trying to set Timer in my Windows Store App.

    public void Start_timer()
    {

        Windows.UI.Xaml.DispatcherTimer timer = new DispatcherTimer();           
        timer.Tick += new Windows.UI.Xaml.EventHandler(timer_Tick);
        timer.Interval = new TimeSpan(00, 1, 1);
        bool enabled = timer.IsEnabled;              // Enable the timer
        timer.Start();                              // Start the timer      
      }

On button click I call above method to set this Timer. But when Eventhandler for Tick is set, I get error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Do we need to handle Timers differently in Windows Store apps?

like image 447
Sap Avatar asked Jan 31 '12 11:01

Sap


1 Answers

The solution is to move the Timer out of the method e.g

private DispatcherTimer timer = new DispatcherTimer();

and set it up in the ctor

public TheClass()
{
    timer.Tick += timer_Tick; 
    timer.Interval = new TimeSpan(00, 1, 1);
    timer.Start();
}

Hard to tell what is the reason without the full code, but it could be the behavior of the timer_Tick.

like image 55
Lukasz Madon Avatar answered Sep 28 '22 08:09

Lukasz Madon