【问题标题】:Thread error Windows10 IoT Core线程错误 Windows 10 IoT Core
【发布时间】:2018-08-21 06:32:30
【问题描述】:

我是 Windows 10 IoT 的新手。

我将使用 DragonBoard 410c 将应用程序作为白板应用程序。

我将按钮连接到 GPIO。

并编码如下,但发生错误。

private void InitGPIO()
{
    var gpio = GpioController.GetDefault();

    if(gpio == null)
    {

        var dialog2 = new MessageDialog("Please Check GPIO");
        dialog2.ShowAsync();

        return;
    }
    BTN_UP = gpio.OpenPin(BTN_UP_NUMBER);

    BTN_UP.SetDriveMode(GpioPinDriveMode.Input);

    BTN_UP.DebounceTimeout = TimeSpan.FromMilliseconds(50);
    BTN_UP.ValueChanged += btn_up_pushed;

    var dialog = new MessageDialog("GPIO Ready");
    dialog.ShowAsync();
}

private void btn_up_pushed(GpioPin sender, GpioPinValueChangedEventArgs e)
{
    int but_width = 0;
    int but_height = 0;

    but_width = (int)cutButton.Width;
    but_height = (int)cutButton.Height;
}

当我按下按钮时,调用 btn_up_pushed()。 但发生如下图错误。

enter image description here

请帮帮我!

【问题讨论】:

    标签: uwp gpio windows-iot-core-10 dragonboard


    【解决方案1】:

    您收到以下异常,因为您在非 UI 线程中访问 UI 元素(cutButton 是 Button 对吗?)。

    您需要将线程从当前执行线程编组到 UI 线程。

    Windows.UI.Core.CoreDispatcher 可以用于此。这是一个例子:

    using Windows.ApplicationModel.Core;
    
        private async void btn_up_pushed(GpioPin sender, GpioPinValueChangedEventArgs e)
        {
            int but_width = 0;
            int but_height = 0;
    
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
                but_width = (int)cutButton.Width;
                but_height = (int)cutButton.Height;
            });
        }
    

    参考:“CoreDispatcher.RunAsync

    【讨论】:

    • 非常感谢。我用你的建议解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2016-12-28
    • 2016-10-08
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多