Add global lock on startup to prevent race conditions

This commit is contained in:
Kamil Trzciński 2022-11-15 17:00:13 +01:00
parent 40be8eee70
commit 6211bc4f42
10 changed files with 109 additions and 58 deletions

View file

@ -1,4 +1,5 @@
using CommonHelpers.FromLibreHardwareMonitor;
using CommonHelpers;
using CommonHelpers.FromLibreHardwareMonitor;
using Microsoft.VisualBasic.Logging;
using PerformanceOverlay.External;
using RTSSSharedMemoryNET;
@ -9,13 +10,15 @@ using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace PerformanceOverlay
{
internal class Controller : IDisposable
{
public const String Title = "Performance Overlay";
public readonly String TitleWithVersion = Title + " v" + Application.ProductVersion.ToString();
public readonly String TitleWithVersion = Title + " v" + System.Windows.Forms.Application.ProductVersion.ToString();
Container components = new Container();
RTSSSharedMemoryNET.OSD? osd;
@ -28,18 +31,12 @@ namespace PerformanceOverlay
"Starts Performance Overlay on Windows startup."
);
LibreHardwareMonitor.Hardware.Computer libreHardwareComputer = new LibreHardwareMonitor.Hardware.Computer
{
IsCpuEnabled = true,
IsGpuEnabled = true,
IsStorageEnabled = true,
IsBatteryEnabled = true
};
public Controller()
{
var contextMenu = new System.Windows.Forms.ContextMenuStrip(components);
Instance.Open(TitleWithVersion, "Global\\PerformanceOverlay");
showItem = new ToolStripMenuItem("&Show OSD");
showItem.Click += ShowItem_Click;
showItem.Checked = Settings.Default.ShowOSD;
@ -222,7 +219,7 @@ namespace PerformanceOverlay
private void ExitItem_Click(object? sender, EventArgs e)
{
Application.Exit();
System.Windows.Forms.Application.Exit();
}
public void Dispose()

View file

@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Navigation;
using static PerformanceOverlay.Sensors;
using CommonHelpers;
namespace PerformanceOverlay
{
@ -288,33 +289,21 @@ namespace PerformanceOverlay
}
}
};
private LibreHardwareMonitor.Hardware.Computer libreHardwareComputer = new LibreHardwareMonitor.Hardware.Computer
{
IsCpuEnabled = true,
IsMemoryEnabled = true,
IsGpuEnabled = true,
IsStorageEnabled = true,
IsBatteryEnabled = true
};
public IList<ISensor> AllHardwareSensors { get; private set; } = new List<ISensor>();
public Sensors()
{
libreHardwareComputer.Open();
}
public void Dispose()
{
libreHardwareComputer.Close();
}
public void Update()
{
var allSensors = new List<ISensor>();
foreach (IHardware hardware in libreHardwareComputer.Hardware)
foreach (IHardware hardware in Instance.HardwareComputer.Hardware)
{
try
{