我们如何在 WinRT 应用程序中设置计时器? [英] How do we set Timers in WinRT app?

查看:15
本文介绍了我们如何在 WinRT 应用程序中设置计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Windows 应用商店应用程序中设置计时器.

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      
      }

点击按钮我调用上面的方法来设置这个定时器.但是当设置 Tick 的 Eventhandler 时,我得到错误试图读取或写入受保护的内存.这通常表明其他内存已损坏."

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."

我们是否需要在 Windows 应用商店应用中以不同方式处理计时器?

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

推荐答案

解决方法是将Timer移出方法例如

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

private DispatcherTimer timer = new DispatcherTimer();

并在ctor中设置

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

如果没有完整的代码,很难说是什么原因,但可能是 timer_Tick 的行为.

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

这篇关于我们如何在 WinRT 应用程序中设置计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆