Tizana
Posts: 2
Joined: Thu Jun 25, 2015 6:13 pm

Can't Update the UI inside the timer callback event

Thu Jun 25, 2015 6:16 pm

Hi,
i recently start learning how to program a C# windows Universal app with Visualstudio for my Raspberry pi2 devices using windows 10 iot.

I start looking for some program and examples to learn how to coding and i found an accelerometer example. this program allows communication between the Raspberry pi2 (Windows10 iot) and an external accelerometer through the I2C serial protocol.

here's the program : https://github.com/ms-iot/samples/tree/ ... rometer/CS

the program start by initializing the I2C protocol for the I2C master devices which is the Raspberry pi board then the program make a write operation to the accelerometer devices through the I2c protocol in order to set the register value.

Finally the program create a periodic timer , this timer is trigged every 100 ms. inside this one the program should read the value of the register that contain the value of the X,Y,Z axis and displayed to the UI.

Code: Select all

/* Now that everything is initialized, create a timer so we read data every 100mS */
            
periodicTimer = new Timer(this.TimerCallback, null, 0, 100);
I notice that every modification affecting the UI inside the timercallback doesn't work. like changing the value of a textblock that i add for a debbuging purposes.
the program use the dispatcher.runAsync inside the Timer callback in order to modify the value of the UI.

Code: Select all

 /* UI updates must be invoked on the UI thread */
            var task = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
              
                 Text_X_Axis.Text = xText;
                 Text_Y_Axis.Text = yText;
                 Text_Z_Axis.Text = zText;
                 Text_Status.Text = statusText;

            });

So if i want to make a change for a text block inside the timercallback i have to add this one inside the dispatcher??

the only think i didn't understand is why the timercallback is considered as a thread ?? because inside the program there are no thread declaration.

can somebody explain me the timercallback and a little bit about thread and dispatcher because i'm new for this. and if you have a good tutorial talking about that it will be grateful. even a french tutorial is OK for me.

markrad_msft
Posts: 58
Joined: Thu May 07, 2015 7:46 pm

Re: Can't Update the UI inside the timer callback event

Mon Jun 29, 2015 11:11 pm

If you want to modify the UI from a timer callback then yes you have to use the Dispatcher to achieve that. The callback that you provide will be executed when the time expires on a thread that is created by the Timer, not the UI thread.

Your other option is to use System.Diagnostics.Debug.WriteLine("string") which will send the "string" to the output window of your attached instance of Visual Studio 2015 assuming you are deploying and debugging the application.
Mark Radbourne | Microsoft | Windows 10 IoT Core Insider Preview Support | This posting is provided 'as is' with no warranties and confers no rights.

haroldpulcher
Posts: 58
Joined: Tue Jun 09, 2015 8:08 pm
Contact: Website

Re: Can't Update the UI inside the timer callback event

Tue Jun 30, 2015 3:30 am

Been a while since I have done this, but if you use INotifyPropertyChanged on your view model, just updating the mode should trigger the screen to update.

Something along these lines from the docs:
https://msdn.microsoft.com/en-us/librar ... .110).aspx

Harold

Return to “Windows 10 for IoT”