YS_PEN
YS_PEN

Reputation: 31

Thread error Windows10 IoT Core

I am a new in Windows 10 IoT.

I will make a application as White Board app with DragonBoard 410c.

I connected push button to GPIO.

And coded as below but error happened.

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

when I pushed the button, called btn_up_pushed(). but error happened as below picture.

enter image description here

Please help me!

Upvotes: 1

Views: 57

Answers (1)

Rita Han
Rita Han

Reputation: 9700

You get the following exception because you access UI element(cutButton is a Button right?) in the non-UI thread.

enter image description here

You need to marshal the thread from the current executing thread to the UI thread.

Windows.UI.Core.CoreDispatcher can be used to this. Here is an example:

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

Ref: "CoreDispatcher.RunAsync"

Upvotes: 1

Related Questions