Skip to content

Commit c87c737

Browse files
committed
fixed battery hardware duplication on reset
1 parent 76b44d6 commit c87c737

7 files changed

Lines changed: 45 additions & 20 deletions

File tree

OpenHardwareMonitor/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void Main()
2727
Environment.Exit(0);
2828
}
2929

30-
if (WinApiHelper.CheckRunningInstances(true, true))
30+
if (!System.Diagnostics.Debugger.IsAttached && WinApiHelper.CheckRunningInstances(true, true))
3131
{
3232
// fallback
3333
MessageBox.Show($"{Updater.ApplicationName} is already running.", Updater.ApplicationName,

OpenHardwareMonitor/UI/AboutBox.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public AboutBox()
1515
lblCopyright.Text = $"Copyright © {Updater.ApplicationName}";
1616
Font = SystemFonts.MessageBoxFont;
1717
lblVersion.Text = $"Version {Application.ProductVersion} {(Environment.Is64BitProcess ? "x64" : "x86")}";
18+
#if DEBUG
19+
lblVersion.Text += " (DEBUG)";
20+
#endif
1821
//using (var icon = Icon.ExtractAssociatedIcon(Updater.CurrentFileLocation))
1922
using (var icon = EmbeddedResources.GetIcon("icon.ico"))
2023
picLogo.Image = icon.GetLargestBitmap();

OpenHardwareMonitor/UI/MainForm.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,19 @@ public MainForm()
5555
{
5656
InitializeComponent();
5757

58-
sensor.WidthChanged += delegate { TreeView_ColumnWidthChanged(this.sensor); };
59-
value.WidthChanged += delegate { TreeView_ColumnWidthChanged(this.value); };
60-
min.WidthChanged += delegate { TreeView_ColumnWidthChanged(this.min); };
61-
max.WidthChanged += delegate { TreeView_ColumnWidthChanged(this.max); };
58+
sensor.WidthChanged += delegate { TreeView_ColumnWidthChanged(sensor); };
59+
value.WidthChanged += delegate { TreeView_ColumnWidthChanged(value); };
60+
min.WidthChanged += delegate { TreeView_ColumnWidthChanged(min); };
61+
max.WidthChanged += delegate { TreeView_ColumnWidthChanged(max); };
6262

6363
_settings = new PersistentSettings();
6464
_settings.Load();
6565

6666
MinimumSize = new Size(400, 200);
6767
Text = Updater.ApplicationTitle;
68+
#if DEBUG
69+
Text += " (DEBUG)";
70+
#endif
6871
Icon = Icon.ExtractAssociatedIcon(Updater.CurrentFileLocation);
6972
portableModeMenuItem.Checked = _settings.IsPortable;
7073

@@ -472,7 +475,10 @@ private void PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEve
472475
{
473476
if (eventArgs.Mode == Microsoft.Win32.PowerModes.Resume || _computer.IsBatteryEnabled)
474477
{
475-
_computer.Reset();
478+
if (InvokeRequired)
479+
Invoke(new MethodInvoker(() => ResetClick(sender, eventArgs)));
480+
else
481+
ResetClick(sender, eventArgs);
476482
}
477483
}
478484

@@ -890,7 +896,7 @@ protected override void WndProc(ref Message m)
890896
WindowState = FormWindowState.Normal;
891897
Activate();
892898
BringToFront();
893-
WinApiHelper.SetForegroundWindow(this.Handle);
899+
WinApiHelper.SetForegroundWindow(Handle);
894900
}
895901
else
896902
{

OpenHardwareMonitorLib/Hardware/Battery/Battery.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public Battery
110110
}
111111
}
112112

113+
public uint BatteryTag => _batteryTag;
114+
113115
public float? ChargeDischargeCurrent { get; private set; }
114116

115117
public float? ChargeDischargeRate { get; private set; }

OpenHardwareMonitorLib/Hardware/Battery/BatteryGroup.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Runtime.InteropServices;
56
using System.Text;
67
using OpenHardwareMonitor.Interop;
@@ -10,9 +11,9 @@ namespace OpenHardwareMonitor.Hardware.Battery;
1011

1112
internal class BatteryGroup : IGroup
1213
{
13-
private readonly List<Battery> _hardware = new();
14+
private readonly List<Battery> _hardware = [];
1415

15-
static bool QueryStringFromBatteryInfo(SafeFileHandle battery, Kernel32.BATTERY_QUERY_INFORMATION bqi, out string value)
16+
private static bool QueryStringFromBatteryInfo(SafeFileHandle battery, Kernel32.BATTERY_QUERY_INFORMATION bqi, out string value)
1617
{
1718
const int maxLoadString = 100;
1819

@@ -100,7 +101,8 @@ public unsafe BatteryGroup(ISettings settings)
100101
ref bqi.BatteryTag,
101102
Marshal.SizeOf(bqi.BatteryTag),
102103
out _,
103-
IntPtr.Zero))
104+
IntPtr.Zero)
105+
&& _hardware.All(x => x.BatteryTag != bqi.BatteryTag))
104106
{
105107
Kernel32.BATTERY_INFORMATION bi = default;
106108
bqi.InformationLevel = Kernel32.BATTERY_QUERY_INFORMATION_LEVEL.BatteryInformation;
@@ -126,6 +128,10 @@ public unsafe BatteryGroup(ISettings settings)
126128
}
127129
}
128130
}
131+
else
132+
{
133+
battery.Close();
134+
}
129135
}
130136
}
131137

OpenHardwareMonitorLib/Hardware/Computer.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,10 @@ public void Accept(IVisitor visitor)
391391
if (visitor == null)
392392
throw new ArgumentNullException(nameof(visitor));
393393

394-
visitor.VisitComputer(this);
394+
lock (_lock)
395+
{
396+
visitor.VisitComputer(this);
397+
}
395398
}
396399

397400
/// <summary>
@@ -671,8 +674,11 @@ public void Reset()
671674
if (!_open)
672675
return;
673676

674-
RemoveGroups();
675-
AddGroups();
677+
lock (_lock)
678+
{
679+
RemoveGroups();
680+
AddGroups();
681+
}
676682
}
677683

678684
private void RemoveGroups()

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Open hardware monitor
1+
# Open Hardware Monitor
2+
23
[![Release](https://img.shields.io/github/v/release/HardwareMonitor/openhardwaremonitor)](https://github.com/HardwareMonitor/openhardwaremonitor/releases/latest)
34
[![Downloads](https://img.shields.io/github/downloads/HardwareMonitor/openhardwaremonitor/total?color=ff4f42)](https://github.com/HardwareMonitor/openhardwaremonitor/releases)
45
[![Last commit](https://img.shields.io/github/last-commit/HardwareMonitor/openhardwaremonitor?color=00AD00)](https://github.com/HardwareMonitor/openhardwaremonitor/commits/master)
@@ -8,9 +9,9 @@
89
[![Nuget](https://img.shields.io/nuget/v/OpenHardwareMonitorLib)](https://www.nuget.org/packages/OpenHardwareMonitorLib/)
910
[![Nuget](https://img.shields.io/nuget/dt/OpenHardwareMonitorLib?label=nuget-downloads)](https://www.nuget.org/packages/OpenHardwareMonitorLib/)
1011

11-
Open hardware monitor is free software that can monitor the temperature sensors, fan speeds, voltages, load and clock speeds of your computer.
12+
Open Hardware Monitor is a free open source software that can monitor temperature sensors, fan speeds, voltages, load and clock speeds of a computer.
1213

13-
This application is based on the "original" [openhardwaremonitor](https://github.com/openhardwaremonitor/openhardwaremonitor) project.
14+
This application is based on the original [OpenHardwareMonitor](https://github.com/openhardwaremonitor/openhardwaremonitor) project.
1415

1516
## Features
1617

@@ -140,8 +141,9 @@ Some sensors require administrator privileges to access the data. Restart your I
140141

141142

142143
## How can I help improve it?
143-
The OpenHardwareMonitor team welcomes feedback and contributions!<br/>
144-
You can check if it works properly on your motherboard. For many manufacturers, the way of reading data differs a bit, so if you notice any inaccuracies, please send us a pull request. If you have any suggestions or improvements, don't hesitate to create an issue.
144+
Your feedback and contributions are welcome! Please feel free to submit Pull Requests or report issues.
145+
<br/>
146+
Please check if it works properly on your hardware. For many manufacturers, the way of reading data differs a bit, so if you notice any inaccuracies, please send us a pull request. If you have any suggestions or improvements, don't hesitate to create an issue.
145147

146148
Also, don't forget to ★ star ★ the repository to help other people find it.
147149

@@ -151,8 +153,8 @@ Also, don't forget to ★ star ★ the repository to help other people find it.
151153

152154
[![Forkers](https://reporoster.com/forks/HardwareMonitor/OpenHardwareMonitor)](https://github.com/HardwareMonitor/OpenHardwareMonitor/network/members)
153155

154-
## Donate!
155-
Every [cup of coffee](https://patreon.com/SergiyE) you donate will help this app become better and let me know that this project is in demand.
156+
## Donate
157+
If you find this project useful, consider [supporting the author](https://patreon.com/SergiyE).
156158

157159
## License
158160

0 commit comments

Comments
 (0)