首页 > 解决方案 > 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以查看错误发生在哪一行。在控制台中我可以看到test1test2因此错误必须发生在发生错误的地方DispatcherTimer。除了以下内容,我没有看到任何错误或异常:

抛出异常:[程序名称].exe 中的“System.Exception”

我做了一些研究,发现了另一个计时器ThreadPoolTimer,但是这个 A) 不能按规定工作,B) 不符合我的要求。

什么是我的问题的解决方案?

编辑

这就是我想用计时器做的事情:

while (true)
{
    if (!updatePositionTimer.IsEnabled)
    {
        updatePositionTimer.Start();
    }

    Sleep(TimeSpan.FromMilliseconds(2));
}

标签: c#uwpraspberry-piwindows-10-iot-core

解决方案


推荐阅读