Continuously update UWP UI elements using multiple threads (Windows 10 IoT Core)

123 Views Asked by At

I have a UWP Application running on Windows 10 IoT Core where I am continuously reading outputs from multiple sensors, and displaying them in a TextBlock.

I am currently using a DispatcherTimer for each sensor, but I wanted to see if there's a way to optimize the code by using multiple threads (Each thread would gather the data from a sensor, and then the UI thread would update the TextBlocks)

I tried to write the following code but it made things way worse

This is the timer I created for one of the sensors:

    private void updatePeakValue()
    {
        timerPeakValue = new System.Timers.Timer(10);
        timerPeakValue.Elapsed += PeakValue_Tick;
        timerPeakValue.Enabled = true;
        timerPeakValue.Start();
    }

And here is the Elapsed Event (I am using rand.next to mimic reading values from a sensor):

    private async void PeakValue_Tick(object sender, object e)
    {
        Random rand = new Random();
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
            tbPeakValue.Text = rand.Next(0, 10).ToString();
        });
    }

This code clogs up my UI, the TextBlock only gets updated when I move my mouse

Is there anything else I could try instead of relying on multiple DispatcherTimers?

Thank you in advance!

0

There are 0 best solutions below