线程错误 Windows10 IoT 核心
Thread error Windows10 IoT Core
我是 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
请帮帮我!
您得到以下异常是因为您在非 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;
});
}
我是 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
请帮帮我!
您得到以下异常是因为您在非 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;
});
}