properly dispose of managementObjects

#6-gpu-reporting
sleevezipper 4 years ago
parent cfdf803a7b
commit 4913987727

@ -34,7 +34,8 @@ namespace hass_workstation_service.Domain.Sensors
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
public override string GetState() public override string GetState()
{ {
ManagementObjectCollection collection = _searcher.Get(); using (ManagementObjectCollection collection = _searcher.Get())
{
List<int> processorLoadPercentages = new List<int>(); List<int> processorLoadPercentages = new List<int>();
foreach (ManagementObject mo in collection) foreach (ManagementObject mo in collection)
{ {
@ -46,5 +47,7 @@ namespace hass_workstation_service.Domain.Sensors
double average = processorLoadPercentages.Count > 0 ? processorLoadPercentages.Average() : 0.0; double average = processorLoadPercentages.Count > 0 ? processorLoadPercentages.Average() : 0.0;
return average.ToString("#.##", CultureInfo.InvariantCulture); return average.ToString("#.##", CultureInfo.InvariantCulture);
} }
}
} }
} }

@ -12,9 +12,11 @@ namespace hass_workstation_service.Domain.Sensors
{ {
public class CurrentVolumeSensor : AbstractSensor 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) { 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() 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() public override string GetState()
{ {
var collection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eRender, DEVICE_STATE.DEVICE_STATE_ACTIVE);
List<float> peaks = new List<float>(); List<float> peaks = new List<float>();
foreach (MMDevice device in devices)
foreach (MMDevice device in collection)
{ {
peaks.Add(device.AudioMeterInformation.PeakValues[0]); peaks.Add(device.AudioMeterInformation.PeakValues[0]);
} }

@ -17,7 +17,8 @@ namespace hass_workstation_service.Domain.Sensors
} }
public override string GetState() public override string GetState()
{ {
ManagementObjectCollection collection = _searcher.Get(); using (ManagementObjectCollection collection = _searcher.Get())
{
UInt64? totalMemory = null; UInt64? totalMemory = null;
UInt64? freeMemory = null; UInt64? freeMemory = null;
foreach (ManagementObject mo in collection) foreach (ManagementObject mo in collection)
@ -34,6 +35,8 @@ namespace hass_workstation_service.Domain.Sensors
} }
return ""; return "";
} }
}
public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig() public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig()
{ {
return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel()

@ -92,6 +92,8 @@ namespace hass_workstation_service.Domain.Sensors
} }
var numberOflogonUIProcesses = Process.GetProcessesByName("LogonUI").Length; var numberOflogonUIProcesses = Process.GetProcessesByName("LogonUI").Length;
if (numberOflogonUIProcesses >= numberOfUserDesktops) if (numberOflogonUIProcesses >= numberOfUserDesktops)

@ -34,8 +34,8 @@ namespace hass_workstation_service.Domain.Sensors
public override string GetState() public override string GetState()
{ {
ManagementObjectCollection collection = _searcher.Get(); using (ManagementObjectCollection collection = _searcher.Get())
{
foreach (ManagementObject mo in collection) foreach (ManagementObject mo in collection)
{ {
foreach (PropertyData property in mo.Properties) foreach (PropertyData property in mo.Properties)
@ -45,6 +45,7 @@ namespace hass_workstation_service.Domain.Sensors
} }
return ""; return "";
} }
}
} }
} }

Loading…
Cancel
Save