Update UI from another GPIOListener class

0

I'm developing an application with windows-10-t platform on raspberry-pi3. The application has several pages and listens GPIO ports asyncrhonously in the background. It collects data from GPIO and sends to the WCF-Service, after a bit the UI should be updated by the data coming from the WCFService. I've also tried using Tasks, Dispatcher.Invoke etc. but nothing worked properly. I can collect data coming from GPIO but cannot update UI. What am I doing wrong?

Here is the background GPIO listener class with static variables (I'm listening GPIO in other pages too.):

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

And here is the page that contains the UI to be updated:

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

EDIT: I forgot to add the exception. "The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))" exception is thrown at AddData function called in DataPin_Valuechanged.

c#
asynchronous
gpio
raspberry-pi3
windows-10-iot-core
asked on Stack Overflow Dec 9, 2016 by aozan88 • edited Dec 9, 2016 by aozan88

1 Answer

0

I found the solution in here : https://stackoverflow.com/a/27698035/1093584

Here is the new update-UI function :

public void GPIO_ProgressUpdate(object sender, EventArgs e)
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
    {
        service_data = await Service.wsClient.GetDataFromServicetoUpdateUI(parameter);
        // UI-update
        txtUpdate.Text = service_data.ToString();
    });
}
answered on Stack Overflow Dec 9, 2016 by aozan88 • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0