Hardware/Computer.cs
changeset 83 3fdadd4a830f
parent 64 15181001ee61
child 85 ec4ccaa1210d
     1.1 --- a/Hardware/Computer.cs	Fri Mar 26 20:58:10 2010 +0000
     1.2 +++ b/Hardware/Computer.cs	Sat Mar 27 12:57:09 2010 +0000
     1.3 @@ -40,12 +40,13 @@
     1.4  using System.IO;
     1.5  using System.Globalization;
     1.6  using System.Text;
     1.7 +using System.Threading;
     1.8  
     1.9  namespace OpenHardwareMonitor.Hardware {
    1.10  
    1.11 -  public delegate void HardwareEventHandler(IHardware hardware);
    1.12 +  public class Computer : IComputer {
    1.13  
    1.14 -  public class Computer {
    1.15 +    private Timer timer;
    1.16  
    1.17      private List<IGroup> groups = new List<IGroup>();
    1.18  
    1.19 @@ -61,7 +62,7 @@
    1.20        groups.Add(group);
    1.21  
    1.22        if (HardwareAdded != null)
    1.23 -        foreach (IHardware hardware in group.Hardware) 
    1.24 +        foreach (IHardware hardware in group.Hardware)
    1.25            HardwareAdded(hardware);
    1.26      }
    1.27  
    1.28 @@ -72,7 +73,7 @@
    1.29        groups.Remove(group);
    1.30  
    1.31        if (HardwareRemoved != null)
    1.32 -        foreach (IHardware hardware in group.Hardware) 
    1.33 +        foreach (IHardware hardware in group.Hardware)
    1.34            HardwareRemoved(hardware);
    1.35      }
    1.36  
    1.37 @@ -86,10 +87,24 @@
    1.38        Add(new Nvidia.NvidiaGroup());
    1.39        Add(new TBalancer.TBalancerGroup());
    1.40  
    1.41 -      if (hddEnabled)        
    1.42 +      if (hddEnabled)
    1.43          Add(new HDD.HDDGroup());
    1.44  
    1.45        open = true;
    1.46 +
    1.47 +      timer = new Timer(
    1.48 +        delegate(Object stateInfo) {
    1.49 +          #if !DEBUG
    1.50 +          try {
    1.51 +          #endif
    1.52 +            Update();
    1.53 +          #if !DEBUG
    1.54 +          } catch (Exception exception) {
    1.55 +            Utilities.CrashReport.Save(exception);
    1.56 +            throw;
    1.57 +          }
    1.58 +          #endif
    1.59 +        }, null, 1000, 1000);
    1.60      }
    1.61  
    1.62      private void SubHardwareUpdate(IHardware hardware) {
    1.63 @@ -99,12 +114,14 @@
    1.64        }
    1.65      }
    1.66  
    1.67 -    public void Update() {
    1.68 +    private void Update() {
    1.69        foreach (IGroup group in groups)
    1.70          foreach (IHardware hardware in group.Hardware) {
    1.71            hardware.Update();
    1.72            SubHardwareUpdate(hardware);
    1.73          }
    1.74 +      if (Updated != null)
    1.75 +        Updated();
    1.76      }
    1.77  
    1.78      public bool HDDEnabled {
    1.79 @@ -118,17 +135,19 @@
    1.80              if (group is HDD.HDDGroup)
    1.81                list.Add(group);
    1.82            foreach (IGroup group in list)
    1.83 -            Remove(group);          
    1.84 +            Remove(group);
    1.85          }
    1.86          hddEnabled = value;
    1.87        }
    1.88      }
    1.89  
    1.90 -    public IEnumerable<IHardware> Hardware {
    1.91 -      get {       
    1.92 +    public IHardware[] Hardware {
    1.93 +      get {
    1.94 +        List<IHardware> list = new List<IHardware>();
    1.95          foreach (IGroup group in groups)
    1.96            foreach (IHardware hardware in group.Hardware)
    1.97 -            yield return hardware;
    1.98 +            list.Add(hardware);
    1.99 +        return list.ToArray();
   1.100        }
   1.101      }
   1.102  
   1.103 @@ -139,11 +158,10 @@
   1.104        writer.WriteLine();
   1.105      }
   1.106  
   1.107 -    private void ReportHardwareTree(IHardware hardware, TextWriter w, 
   1.108 -      string space) 
   1.109 -    {
   1.110 +    private void ReportHardwareTree(IHardware hardware, TextWriter w,
   1.111 +      string space) {
   1.112        w.WriteLine("{0}|", space);
   1.113 -      w.WriteLine("{0}+-+ {1} ({2})", 
   1.114 +      w.WriteLine("{0}+-+ {1} ({2})",
   1.115          space, hardware.Name, hardware.Identifier);
   1.116        foreach (ISensor sensor in hardware.Sensors) {
   1.117          w.WriteLine("{0}|   +- {1} : {2} : {3} : {4}",
   1.118 @@ -171,15 +189,16 @@
   1.119          ReportHardware(subHardware, w);
   1.120      }
   1.121  
   1.122 -    public void SaveReport(Version version) {
   1.123 +    public string GetReport() {
   1.124  
   1.125 -      using (TextWriter w =
   1.126 -        new StreamWriter("OpenHardwareMonitor.Report.txt")) {        
   1.127 +      using (StringWriter w = new StringWriter()) {
   1.128  
   1.129          w.WriteLine();
   1.130          w.WriteLine("Open Hardware Monitor Report");
   1.131          w.WriteLine();
   1.132  
   1.133 +        Version version = typeof(Computer).Assembly.GetName().Version;
   1.134 +
   1.135          NewSection(w);
   1.136          w.Write("Version: "); w.WriteLine(version.ToString());
   1.137          w.WriteLine();
   1.138 @@ -187,7 +206,7 @@
   1.139          NewSection(w);
   1.140          foreach (IGroup group in groups) {
   1.141            foreach (IHardware hardware in group.Hardware)
   1.142 -            ReportHardwareTree(hardware, w, "");          
   1.143 +            ReportHardwareTree(hardware, w, "");
   1.144          }
   1.145          w.WriteLine();
   1.146  
   1.147 @@ -201,12 +220,16 @@
   1.148            IHardware[] hardwareArray = group.Hardware;
   1.149            foreach (IHardware hardware in hardwareArray)
   1.150              ReportHardware(hardware, w);
   1.151 -          
   1.152 +
   1.153          }
   1.154 +        return w.ToString();
   1.155        }
   1.156      }
   1.157  
   1.158 -    public void Close() {
   1.159 +    public void Close() {      
   1.160 +      timer.Dispose();
   1.161 +      timer = null;
   1.162 +
   1.163        if (!open)
   1.164          return;
   1.165  
   1.166 @@ -217,6 +240,7 @@
   1.167        open = false;
   1.168      }
   1.169  
   1.170 +    public event UpdateEventHandler Updated;
   1.171      public event HardwareEventHandler HardwareAdded;
   1.172      public event HardwareEventHandler HardwareRemoved;
   1.173