我们如何设置的WinRT应用计时? [英] How do we set Timers in WinRT app?

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

问题描述

我试图设置计时器在我的Windows Store应用。

 公共无效START_TIMER()
{

Windows.UI.Xaml.DispatcherTimer计时器=新DispatcherTimer();
timer.Tick + =新Windows.UI.Xaml.EventHandler(timer_Tick);
timer.Interval =新时间跨度(00,1,1);
布尔启用= timer.IsEnabled; //启用定时器
timer.Start(); //启动定时器
}

在点击链接我叫上面的方法设置定时器。但是,当事件处理程序蜱设置,我得到错误
尝试读取或写入受保护的内存。这通常是指示其他内存已损坏。



我们是否需要在Windows Store应用程序不同的方式处理计时?


解决方案

解决方案是移动定时器该方法如:



 私人DispatcherTimer定时器=新DispatcherTimer(); 

和在构造函数



<$设置它p $ p> 公共theClass描述()
{
timer.Tick + = timer_Tick;
timer.Interval =新时间跨度(00,1,1);
timer.Start();
}



很难说是不完整的代码的原因,但也可能是该timer_Tick的行为。


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?

解决方案

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.

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

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