How do we set Timers in WinRT app?(我们如何在 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 应用程序中设置计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我们如何在 WinRT 应用程序中设置计时器?
基础教程推荐
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 将数据集转换为列表 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
