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 应用程序中设置计时器?


- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01