Program.cs
author moel.mich
Sun, 15 May 2011 17:21:27 +0000
changeset 286 48d7e8d6c0db
parent 253 0044b05a3094
child 291 61c3d984fb2d
permissions -rw-r--r--
Fixed Issue 204.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.IO;
    40 using System.Reflection;
    41 using System.Text;
    42 using System.Threading;
    43 using System.Windows.Forms;
    44 using OpenHardwareMonitor.GUI;
    45 using OpenHardwareMonitor.Hardware;
    46 
    47 namespace OpenHardwareMonitor {
    48   public static class Program {
    49 
    50     [STAThread]
    51     public static void Main() {
    52       #if !DEBUG
    53         Application.ThreadException += 
    54           new ThreadExceptionEventHandler(Application_ThreadException);
    55         Application.SetUnhandledExceptionMode(
    56           UnhandledExceptionMode.CatchException);
    57 
    58         AppDomain.CurrentDomain.UnhandledException += 
    59           new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    60       #endif
    61 
    62       if (!AllRequiredFilesAvailable())
    63         Environment.Exit(0);
    64 
    65       Application.EnableVisualStyles();
    66       Application.SetCompatibleTextRenderingDefault(false);
    67       using (GUI.MainForm form = new GUI.MainForm()) {
    68         form.FormClosed += delegate(Object sender, FormClosedEventArgs e) {
    69           Application.Exit();
    70         };        
    71         Application.Run();
    72       }
    73     }
    74 
    75     private static bool IsFileAvailable(string fileName) {
    76       string path = Path.GetDirectoryName(Application.ExecutablePath) +
    77         Path.DirectorySeparatorChar;
    78 
    79       if (!File.Exists(path + fileName)) {
    80         MessageBox.Show("The following file could not be found: " + fileName + 
    81           "\nPlease extract all files from the archive.", "Error",
    82            MessageBoxButtons.OK, MessageBoxIcon.Error);
    83         return false;
    84       }
    85       return true;      
    86     }
    87 
    88     private static bool AllRequiredFilesAvailable() {
    89       if (!IsFileAvailable("Aga.Controls.dll"))
    90         return false;
    91       if (!IsFileAvailable("OpenHardwareMonitorLib.dll"))
    92         return false;
    93 
    94       // check if the OpenHardwareMonitorLib assembly has the correct version
    95       if (Assembly.GetAssembly(typeof(Computer)).GetName().Version !=
    96         Assembly.GetExecutingAssembly().GetName().Version) {
    97         MessageBox.Show(
    98           "The version of the file OpenHardwareMonitorLib.dll is incompatible.", 
    99           "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   100         return false;
   101       }
   102 
   103       return true;
   104     }
   105 
   106     private static void ReportException(Exception e) {
   107       CrashForm form = new CrashForm();
   108       form.Exception = e;
   109       form.ShowDialog();
   110     }
   111 
   112     public static void Application_ThreadException(object sender, 
   113       ThreadExceptionEventArgs e) 
   114     {
   115       try {
   116         ReportException(e.Exception);
   117       } catch {
   118       } finally {
   119         Application.Exit();
   120       }
   121     }
   122 
   123     public static void CurrentDomain_UnhandledException(object sender, 
   124       UnhandledExceptionEventArgs args) 
   125     {
   126       try {
   127         Exception e = args.ExceptionObject as Exception;
   128         if (e != null)
   129           ReportException(e);
   130       } catch {
   131       } finally {
   132         Environment.Exit(0);
   133       }
   134     }   
   135   }
   136 }