ValueChanged没有用C#Win10点燃 [英] ValueChanged not firing with C# Win10 Iot

查看:218
本文介绍了ValueChanged没有用C#Win10点燃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来像 Win10 IoT - RaspBerry Pi2 :当GPIO发生变化时,ValueChanged不被调用$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ p> public sealed class StartupTask:IBackgroundTask
{
private const int SENSOR_PIN = 17;
私人GpioPin pinSensor;

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

var gpio = GpioController.GetDefault();

if(gpio!= null)
{
pinSensor = gpio.OpenPin(SENSOR_PIN); //也尝试过GpioSharingMode.SharedReadOnly

var r = pinSensor.Read(); //如果传感器发生变化,就进行更改用quickwatch验证

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

pinSensor.ValueChanged + = PinIn_ValueChanged;
}
}

private void PinIn_ValueChanged(GpioPin sender,GpioPinValueChangedEventArgs args)
{
//从未得到...
}

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

带领传感器和快速表示GpioPinValue在高和低...所以应该得到打击...



当我将其设置为输入后检索驱动器模式。它告诉我它实际上被设置为输入:

  var dm = pinSensor.GetDriveMode(); 

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

解决方案


当Run方法结束时,除非创建延期对象,
背景应用程序结束。异步
编程的常见做法是采取如下推迟:




  var deferval = taskInstance.GetDeferral(); 

参考:开发背景应用程序


It seems exactly like Win10 IoT - RaspBerry Pi2: ValueChanged not called when GPIO change I have a raspberry pi 2 with win10 IoT (creator version) and have this C# code:

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();
    }
}

led on sensor and quickwatch say the GpioPinValue does alternate between high and low... so should get hit...

When I retrieve the drive mode after setting it to input. It tells me it actually is set to input:

var dm = pinSensor.GetDriveMode();

as was suggested in the comment of the linked stack overflow issue. So what am I doing wrong? And more important: why?

解决方案

When the Run method ends, unless a deferral object is created, the Background Application ends. The common practice, for asynchronous programming is to take a deferral like this:

var deferval = taskInstance.GetDeferral();

Ref:Developing Background Applications

这篇关于ValueChanged没有用C#Win10点燃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆