Skip to content

Commit 213c23b

Browse files
committed
try/catch and disable hardware if exception is thrown
1 parent 182fad2 commit 213c23b

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

HardwareMonitor/HardwareMonitor/Monitor/MonitorPoller.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
5050

5151
var sharedMemoryData = QueryHardwareData();
5252

53-
var sensorValueOffset = new Dictionary<int, int>();
54-
5553
using var memoryStream = new MemoryStream();
5654
using var writer = new BinaryWriter(memoryStream);
5755
var accumulator = 0;
@@ -69,7 +67,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
6967

7068
foreach (var hardware in sharedMemoryData.Hardwares)
7169
{
72-
hardware.Hardware.Update();
70+
try
71+
{
72+
hardware.Update();
73+
}
74+
catch
75+
{
76+
hardware.StopUpdates();
77+
logger.LogError("Stopping updates of {HardwareName} - {HardwareIdentifier}", hardware.Name, hardware.Identifier);
78+
}
7379
}
7480

7581
WriteDataToStream(writer, sharedMemoryData);
@@ -267,7 +273,7 @@ private static byte[] GetBytes(string str, int length)
267273
{
268274
return Encoding.UTF8.GetBytes(str.Length > length ? str[..length] : str.PadRight(length, '\0'));
269275
}
270-
276+
271277
public static string RemoveSpecialCharacters(string str)
272278
{
273279
return Regex.Replace(str, "[^a-zA-Z0-9_ .]+", "_", RegexOptions.Compiled);

HardwareMonitor/HardwareMonitor/SharedMemory/SharedMemory.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text.Json.Serialization;
2-
using LibreHardwareMonitor.Hardware;
1+
using LibreHardwareMonitor.Hardware;
32

43
namespace HardwareMonitor.SharedMemory;
54

@@ -36,9 +35,19 @@ public class SharedMemoryHardware
3635
public required string Identifier { get; set; }
3736
public required HardwareType HardwareType { get; set; }
3837

39-
public bool ShouldSerializeHardware()
38+
private bool _isActive = true;
39+
40+
public void Update()
4041
{
41-
return false;
42+
if (_isActive)
43+
{
44+
Hardware.Update();
45+
}
46+
}
47+
48+
public void StopUpdates()
49+
{
50+
_isActive = false;
4251
}
4352
}
4453

@@ -50,24 +59,11 @@ public class SharedMemorySensor
5059
public required string HardwareIdentifier { get; set; }
5160
public required SensorType SensorType { get; set; }
5261
public required float Value { get; set; }
53-
54-
public bool ShouldSerializeSensor()
55-
{
56-
return false;
57-
}
5862
};
5963

6064
public class SharedMemoryData
6165
{
6266
public long LastPollTime { get; set; }
6367
public List<SharedMemoryHardware> Hardwares { get; set; } = [];
6468
public List<SharedMemorySensor> Sensors { get; set; } = [];
65-
}
66-
67-
[JsonSourceGenerationOptions(WriteIndented = false, GenerationMode = JsonSourceGenerationMode.Serialization)]
68-
[JsonSerializable(typeof(SharedMemoryData))]
69-
[JsonSerializable(typeof(SharedMemorySensor))]
70-
[JsonSerializable(typeof(SharedMemoryHardware))]
71-
internal partial class SerializeContext : JsonSerializerContext
72-
{
7369
}

0 commit comments

Comments
 (0)