2

I have tried a lot but I can't find out how to update a GUI element for example a TextBlock.Text from an running Task on Windows Universal App for Windows IoT on Raspberry.

Is there any way to do this?

It should work out of the running task without stopping it.

According to an answer I have tried this:

Task t1 = new Task(() =>
        {
            while (1 == 1)
            {

                byte[] writeBuffer = { 0x41, 0x01, 0 }; // Buffer to write to mcp23017
                byte[] readBuffer = new byte[3]; // Buffer to read to mcp23017
                SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer); // Send writeBuffer to mcp23017 and receive Results to readBuffer
                byte readBuffer2 = readBuffer[2]; // extract the correct result
                string output = Convert.ToString(readBuffer2, 2).PadLeft(8, '0'); // convert result to output Format

                // Update the frontend TextBlock status5 with result
                Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () =>
                {
                    // Your UI update code goes here!
                    status6.Text = output;
                });

            }
        });
        t1.Start(); 

But I get the following 2 errors:

Error   CS0103  The name 'CoreDispatcherPriority' does not exist in the current context

and

CS4014  Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Am I doing something wrong using the code?

Denis
  • 71
  • 9
  • 1
    I am not experienced with Windows-universal, but in winforms/wpf there is an `Invoke` method to do that. As well as an asynchronous version (`InvokeAsync`/`BeginInvoke`). – Sinatr Aug 03 '15 at 09:04
  • I have tried it with invoke but this is not running under Universal Windows Apps. – Denis Aug 03 '15 at 10:00
  • CS0103: add using Windows.UI.Core (see edited sample below) CS4014: add await keyword and mark scope async (see edited sample below) – Daniel Meixner Aug 03 '15 at 11:49

1 Answers1

1

I'm not sure where your problem is, I guess it might be a problem with different threads. Try to use the dispatcher. You need to integrate the Windows.UI.Core namespace:

using Windows.UI.Core;

Here's your call (slightly modified to work out of the box).

private void DoIt()
        {

            Task t1 = new Task(async () =>
            {
                while (1 == 1)
                {
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                        () =>
                                        {
                                            // Your UI update code goes here!
                                            status6.Text = "Hello" + DateTime.Now;
                                        });
                    await Task.Delay(1000);
                }
            });
            t1.Start();

        }  

Little hint: while (1=1) sounds like an infinite loop to me. Another hint: I added "await Task.Delay(1000);" to have a little break during the loops.

Also check out this answer regarding dispatcher. Correct way to get the CoreDispatcher in a Windows Store app

Community
  • 1
  • 1
Daniel Meixner
  • 1,829
  • 11
  • 10
  • Thx for your help, I have tried it to use the code from you but it won't work (I have added the code in the question). – Denis Aug 03 '15 at 10:28