I have a windows gaming handheld tool program I am creating that has several windows (like main window, a quick access menu window, etc). Many of these windows display several system values such as battery %, charging status, and the current thermal design power (TDP) of the CPU. Because some of the values require using external programs like RyzenAdj.exe and RW.exe that read the computer MMIO and cpu MSR, I don't want each window to manage this. Accidentally calling RyzenAdj.exe from two threads causes the exe to lock up.
To make everything easier on the UI thread, I have a dedicated background thread that loops through and gets these values every few seconds and posts them to a public class of variables. I then have dispatch timers in each window that checks the public class every few seconds for changes, but this seems like overkill and an event handler would be better suited.
Here is an example of how my background thread collecting values does
while (programRunning == true) { PublicVariables.networkStatus = getNetworkStatus(); PublicVariables.currentTDP = readCurrentTDP(); PublicVariables.batteryPercentage = readCurrentBatteryPercentage(): Thread.Sleep(1000); } In the different windows I have a dispatch timer that on tick says
labelTDP = PublicVariables.currentTDP; labelNetworkStatus = PublicVariables.networkStatus; This feels like i'm using unnecessary timers. I think I want an event handler, but i'm also concerned that using an event handler and having multiple subscribers could initiate multiple instances of the class that reads tdp, and could cause the RyzenAdj.exe program to stop.
Do you have any advice on how to handle this?