c# - DispatcherTimer 在 uwp 应用程序中不起作用
问题描述
我在我的 Raspberry Pi(Windows 10 IoT Core)上运行了一个 UWP 应用程序。我需要一段时间来启动、停止、检查它是否正在运行并在之后执行某些操作。这DispatcherTimer
似乎是一个完美的契合,如果它会运行......
这是我的代码:
public override void Run()
{
Debug.WriteLine("test1");
SenseHat.Display.Clear();
Debug.WriteLine("test2");
_currentDirection = Direction.East;
updatePositionTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(500) };
Debug.WriteLine("test3");
updatePositionTimer.Tick += timer_Tick;
Debug.WriteLine("test4");
我添加了Writelines
以查看错误发生在哪一行。在控制台中我可以看到test1
,test2
因此错误必须发生在发生错误的地方DispatcherTimer
。除了以下内容,我没有看到任何错误或异常:
抛出异常:[程序名称].exe 中的“System.Exception”
我做了一些研究,发现了另一个计时器ThreadPoolTimer,但是这个 A) 不能按规定工作,B) 不符合我的要求。
什么是我的问题的解决方案?
编辑
这就是我想用计时器做的事情:
while (true)
{
if (!updatePositionTimer.IsEnabled)
{
updatePositionTimer.Start();
}
Sleep(TimeSpan.FromMilliseconds(2));
}
解决方案
推荐阅读
- php - 从php中的数组中检索一个单词
- sql - SQL查询一批地址
- sql - 平均库存天数
- jquery - Jquery:ID 匹配正则表达式或过滤器的所有输入?
- docker - Nginx over TLS 与 docker compose
- jsp - 访问 .jsp 文件中的 java 映射
- powerapps - 在 Power Apps 中的按钮选项卡上按下时,如何仅显示共享点列表中的某些列值?
- javascript - Time Series Insights Gen 2 - 如何使用 tsiClient,,server.GetEvents 调用 Get Events
- javascript - 如何在 SearchKit 中强制更改查询?
- r - 在闪亮的应用程序中将轴值作为输入时不显示条形图