Solution Build Config update.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2010 Paul Werelds <paul@werelds.net>
8 Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
13 using System.Collections.Generic;
14 using System.Management.Instrumentation;
15 using OpenHardwareMonitor.Hardware;
17 [assembly: Instrumented("root/OpenHardwareMonitor")]
19 [System.ComponentModel.RunInstaller(true)]
20 public class InstanceInstaller : DefaultManagementProjectInstaller { }
22 namespace OpenHardwareMonitor.WMI {
25 /// This class is not exposed to WMI itself.
27 public class WmiProvider : IDisposable {
28 private List<IWmiObject> activeInstances;
30 public WmiProvider(IComputer computer) {
31 activeInstances = new List<IWmiObject>();
33 foreach (IHardware hardware in computer.Hardware)
34 ComputerHardwareAdded(hardware);
36 computer.HardwareAdded += ComputerHardwareAdded;
37 computer.HardwareRemoved += ComputerHardwareRemoved;
40 public void Update() {
41 foreach (IWmiObject instance in activeInstances)
47 private void ComputerHardwareAdded(IHardware hardware) {
48 if (!Exists(hardware.Identifier.ToString())) {
49 foreach (ISensor sensor in hardware.Sensors)
50 HardwareSensorAdded(sensor);
52 hardware.SensorAdded += HardwareSensorAdded;
53 hardware.SensorRemoved += HardwareSensorRemoved;
55 Hardware hw = new Hardware(hardware);
56 activeInstances.Add(hw);
59 Instrumentation.Publish(hw);
60 } catch (Exception) { }
63 foreach (IHardware subHardware in hardware.SubHardware)
64 ComputerHardwareAdded(subHardware);
67 private void HardwareSensorAdded(ISensor data) {
68 Sensor sensor = new Sensor(data);
69 activeInstances.Add(sensor);
72 Instrumentation.Publish(sensor);
73 } catch (Exception) { }
76 private void ComputerHardwareRemoved(IHardware hardware) {
77 hardware.SensorAdded -= HardwareSensorAdded;
78 hardware.SensorRemoved -= HardwareSensorRemoved;
80 foreach (ISensor sensor in hardware.Sensors)
81 HardwareSensorRemoved(sensor);
83 foreach (IHardware subHardware in hardware.SubHardware)
84 ComputerHardwareRemoved(subHardware);
86 RevokeInstance(hardware.Identifier.ToString());
89 private void HardwareSensorRemoved(ISensor sensor) {
90 RevokeInstance(sensor.Identifier.ToString());
97 private bool Exists(string identifier) {
98 return activeInstances.Exists(h => h.Identifier == identifier);
101 private void RevokeInstance(string identifier) {
102 int instanceIndex = activeInstances.FindIndex(
103 item => item.Identifier == identifier.ToString()
106 if (instanceIndex == -1)
110 Instrumentation.Revoke(activeInstances[instanceIndex]);
111 } catch (Exception) { }
113 activeInstances.RemoveAt(instanceIndex);
118 public void Dispose() {
119 foreach (IWmiObject instance in activeInstances) {
121 Instrumentation.Revoke(instance);
122 } catch (Exception) { }
124 activeInstances = null;