How do we set Timers in WinRT app?

落爺英雄遲暮 提交于 2019-12-30 06:01:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9078704/how-do-we-set-timers-in-winrt-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!