Program.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
parent 344 3145aadca3d2
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
     1 /*
     2  
     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/.
     6  
     7   Copyright (C) 2009-2013 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.IO;
    13 using System.Threading;
    14 using System.Windows.Forms;
    15 using OpenHardwareMonitor.GUI;
    16 
    17 namespace OpenHardwareMonitor {
    18   public static class Program {
    19 
    20     [STAThread]
    21     public static void Main() {
    22       #if !DEBUG
    23         Application.ThreadException += 
    24           new ThreadExceptionEventHandler(Application_ThreadException);
    25         Application.SetUnhandledExceptionMode(
    26           UnhandledExceptionMode.CatchException);
    27 
    28         AppDomain.CurrentDomain.UnhandledException += 
    29           new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    30       #endif
    31 
    32       if (!AllRequiredFilesAvailable())
    33         Environment.Exit(0);
    34 
    35       Application.EnableVisualStyles();
    36       Application.SetCompatibleTextRenderingDefault(false);
    37       using (GUI.MainForm form = new GUI.MainForm()) {
    38         form.FormClosed += delegate(Object sender, FormClosedEventArgs e) {
    39           Application.Exit();
    40         };        
    41         Application.Run();
    42       }
    43     }
    44 
    45     private static bool IsFileAvailable(string fileName) {
    46       string path = Path.GetDirectoryName(Application.ExecutablePath) +
    47         Path.DirectorySeparatorChar;
    48 
    49       if (!File.Exists(path + fileName)) {
    50         MessageBox.Show("The following file could not be found: " + fileName + 
    51           "\nPlease extract all files from the archive.", "Error",
    52            MessageBoxButtons.OK, MessageBoxIcon.Error);
    53         return false;
    54       }
    55       return true;      
    56     }
    57 
    58     private static bool AllRequiredFilesAvailable() {
    59       if (!IsFileAvailable("Aga.Controls.dll"))
    60         return false;
    61       if (!IsFileAvailable("OpenHardwareMonitorLib.dll"))
    62         return false;
    63       if (!IsFileAvailable("OxyPlot.dll"))
    64         return false;
    65       if (!IsFileAvailable("OxyPlot.WindowsForms.dll"))
    66         return false;
    67       
    68       return true;
    69     }
    70 
    71     private static void ReportException(Exception e) {
    72       CrashForm form = new CrashForm();
    73       form.Exception = e;
    74       form.ShowDialog();
    75     }
    76 
    77     public static void Application_ThreadException(object sender, 
    78       ThreadExceptionEventArgs e) 
    79     {
    80       try {
    81         ReportException(e.Exception);
    82       } catch {
    83       } finally {
    84         Application.Exit();
    85       }
    86     }
    87 
    88     public static void CurrentDomain_UnhandledException(object sender, 
    89       UnhandledExceptionEventArgs args) 
    90     {
    91       try {
    92         Exception e = args.ExceptionObject as Exception;
    93         if (e != null)
    94           ReportException(e);
    95       } catch {
    96       } finally {
    97         Environment.Exit(0);
    98       }
    99     }   
   100   }
   101 }