ValueChanged没有使用C#Win10 Iot触发

时间:2017-05-03 14:46:52

标签: c# event-handling gpio windows-10-iot-core

看起来与Win10 IoT - RaspBerry Pi2: ValueChanged not called when GPIO change完全相同 我有一个带有win10 IoT(创建者版本)的覆盆子pi 2并且有这个C#代码:

public sealed class StartupTask : IBackgroundTask
{
    private const int SENSOR_PIN = 17;
    private GpioPin pinSensor;

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        taskInstance.Canceled += TaskInstance_Canceled; // "destructor"

        var gpio = GpioController.GetDefault();

        if (gpio != null)
        {
            pinSensor = gpio.OpenPin(SENSOR_PIN); // also tried with GpioSharingMode.SharedReadOnly

            var r = pinSensor.Read(); // works and changes if sensor changes. Verified with quickwatch

            pinSensor.SetDriveMode(GpioPinDriveMode.Input);
            pinSensor.DebounceTimeout = TimeSpan.FromMilliseconds(20);

            pinSensor.ValueChanged += PinIn_ValueChanged;
        }
    }

    private void PinIn_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
    {
        // never gets hit... 
    }

    private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
    {
        pinSensor.Dispose();
    }
}

以传感器和速度表为主导说GpioPinValue确实在高低之间交替...所以应该受到打击...

设置为输入后检索驱动模式。它告诉我它实际上已设置为输入:

var dm = pinSensor.GetDriveMode();

正如链接堆栈溢出问题的注释中所建议的那样。那么我做错了什么?更重要的是:为什么?

1 个答案:

答案 0 :(得分:1)

  

当Run方法结束时,除非创建了延迟对象,否则   背景申请结束。异步的常见做法   编程就像这样推迟:

var deferval = taskInstance.GetDeferral();

价:Developing Background Applications