From 491398772738bca6ba61e916e8515c28cd17b857 Mon Sep 17 00:00:00 2001 From: sleevezipper Date: Sun, 14 Mar 2021 18:03:42 +0100 Subject: [PATCH] properly dispose of managementObjects --- .../Domain/Sensors/CPULoadSensor.cs | 17 +++++----- .../Domain/Sensors/CurrentVolumeSensor.cs | 14 +++------ .../Domain/Sensors/MemoryUsageSensor.cs | 31 ++++++++++--------- .../Domain/Sensors/SessionStateSensor.cs | 2 ++ .../Domain/Sensors/WMIQuerySensor.cs | 13 ++++---- 5 files changed, 41 insertions(+), 36 deletions(-) diff --git a/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs b/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs index d84fe1f..9fd547b 100644 --- a/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs +++ b/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs @@ -34,17 +34,20 @@ namespace hass_workstation_service.Domain.Sensors [SupportedOSPlatform("windows")] public override string GetState() { - ManagementObjectCollection collection = _searcher.Get(); - List processorLoadPercentages = new List(); - foreach (ManagementObject mo in collection) + using (ManagementObjectCollection collection = _searcher.Get()) { - foreach (PropertyData property in mo.Properties) + List processorLoadPercentages = new List(); + foreach (ManagementObject mo in collection) { - processorLoadPercentages.Add(int.Parse(property.Value.ToString())); + foreach (PropertyData property in mo.Properties) + { + processorLoadPercentages.Add(int.Parse(property.Value.ToString())); + } } + double average = processorLoadPercentages.Count > 0 ? processorLoadPercentages.Average() : 0.0; + return average.ToString("#.##", CultureInfo.InvariantCulture); } - double average = processorLoadPercentages.Count > 0 ? processorLoadPercentages.Average() : 0.0; - return average.ToString("#.##", CultureInfo.InvariantCulture); + } } } diff --git a/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs b/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs index aa5d33d..9dfd7f2 100644 --- a/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs +++ b/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs @@ -12,9 +12,11 @@ namespace hass_workstation_service.Domain.Sensors { public class CurrentVolumeSensor : AbstractSensor { - private MMDeviceEnumerator DevEnum; + private MMDeviceEnumerator deviceEnumerator; + private MMDeviceCollection devices; public CurrentVolumeSensor(MqttPublisher publisher, int? updateInterval = null, string name = "CurrentVolume", Guid id = default(Guid)) : base(publisher, name ?? "CurrentVolume", updateInterval ?? 10, id) { - this.DevEnum = new MMDeviceEnumerator(); + this.deviceEnumerator = new MMDeviceEnumerator(); + this.devices = deviceEnumerator.EnumerateAudioEndPoints(EDataFlow.eRender, DEVICE_STATE.DEVICE_STATE_ACTIVE); } public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig() { @@ -30,17 +32,11 @@ namespace hass_workstation_service.Domain.Sensors }); } - [DllImport("winmm.dll")] - public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume); - public override string GetState() { - var collection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eRender, DEVICE_STATE.DEVICE_STATE_ACTIVE); - List peaks = new List(); - - foreach (MMDevice device in collection) + foreach (MMDevice device in devices) { peaks.Add(device.AudioMeterInformation.PeakValues[0]); } diff --git a/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs b/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs index 6893d0a..7096d59 100644 --- a/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs +++ b/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs @@ -17,22 +17,25 @@ namespace hass_workstation_service.Domain.Sensors } public override string GetState() { - ManagementObjectCollection collection = _searcher.Get(); - UInt64? totalMemory = null; - UInt64? freeMemory = null; - foreach (ManagementObject mo in collection) + using (ManagementObjectCollection collection = _searcher.Get()) { - totalMemory = (UInt64)mo.Properties["TotalVisibleMemorySize"]?.Value; - freeMemory = (UInt64)mo.Properties["FreePhysicalMemory"]?.Value; + UInt64? totalMemory = null; + UInt64? freeMemory = null; + foreach (ManagementObject mo in collection) + { + totalMemory = (UInt64)mo.Properties["TotalVisibleMemorySize"]?.Value; + freeMemory = (UInt64)mo.Properties["FreePhysicalMemory"]?.Value; + } + if (totalMemory != null && freeMemory != null) + { + decimal totalMemoryDec = totalMemory.Value; + decimal freeMemoryDec = freeMemory.Value; + decimal precentageUsed = 100 - (freeMemoryDec / totalMemoryDec) * 100; + return precentageUsed.ToString("#.##", CultureInfo.InvariantCulture); + } + return ""; } - if (totalMemory != null && freeMemory != null) - { - decimal totalMemoryDec = totalMemory.Value; - decimal freeMemoryDec = freeMemory.Value; - decimal precentageUsed = 100 - (freeMemoryDec / totalMemoryDec) * 100; - return precentageUsed.ToString("#.##", CultureInfo.InvariantCulture); - } - return ""; + } public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig() { diff --git a/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs b/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs index 882cbd7..7457ae5 100644 --- a/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs +++ b/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs @@ -92,6 +92,8 @@ namespace hass_workstation_service.Domain.Sensors } + + var numberOflogonUIProcesses = Process.GetProcessesByName("LogonUI").Length; if (numberOflogonUIProcesses >= numberOfUserDesktops) diff --git a/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs b/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs index 6304389..d2601b6 100644 --- a/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs +++ b/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs @@ -34,16 +34,17 @@ namespace hass_workstation_service.Domain.Sensors public override string GetState() { - ManagementObjectCollection collection = _searcher.Get(); - - foreach (ManagementObject mo in collection) + using (ManagementObjectCollection collection = _searcher.Get()) { - foreach (PropertyData property in mo.Properties) + foreach (ManagementObject mo in collection) { - return property.Value.ToString(); + foreach (PropertyData property in mo.Properties) + { + return property.Value.ToString(); + } } + return ""; } - return ""; } }