【发布时间】:2016-12-09 13:08:43
【问题描述】:
我正在 raspberry-pi3 上开发一个带有 windows-10-t 平台的应用程序。该应用程序有几个页面,并在后台异步侦听 GPIO 端口。它从 GPIO 收集数据并发送到 WCF-Service,稍后 UI 应该由来自 WCFService 的数据更新。我也尝试过使用 Tasks、Dispatcher.Invoke 等,但没有任何工作正常。我可以收集来自 GPIO 的数据,但无法更新 UI。我做错了什么?
这是带有静态变量的后台 GPIO 侦听器类(我也在其他页面中侦听 GPIO。):
public sealed class GPIO{
private static MainPage mainpage;
public static event EventHandler ProgressUpdate;
public static void InitGPIO(MainPage sender)
{
mainpage = sender;
DataPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);
DataPin.ValueChanged += DataPin_ValueChanged;
}
public static void DataPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
Task.Run(() => AddData(0));
}
}
public static async void AddData(int prm_Data)
{
// WCF-Service Operation
await Service.wsClient.GPIOValueAddition(prm_Data);
GPIO.ProgressUpdateOperation();
}
private static void ProgressUpdateOperation()
{
mainpage.GPIO_ProgressUpdate(typeof(GPIO), new EventArgs());
}
}
这是包含要更新的 UI 的页面:
public sealed partial class MainPage : Page
{
public MainPage()
{
GPIO.InitGPIO(this);
GPIO.ProgressUpdate += GPIO_ProgressUpdate;
}
public void GPIO_ProgressUpdate(object sender, EventArgs e)
{
// WCF-Service Operation
service_data = (int)Service.wsClient.GetDataFromServicetoUpdateUI(parameter).Result;
// UI-update
txtUpdate.Text = service_data.ToString();
}
}
编辑:我忘了添加例外。 “应用程序调用了一个为不同线程编组的接口。(来自 HRESULT 的异常:0x8001010E (RPC_E_WRONG_THREAD))”异常在 DataPin_Valuechanged 中调用的 AddData 函数中引发。
【问题讨论】:
标签: c# asynchronous gpio raspberry-pi3 windows-10-iot-core