Program.cs
author moel.mich
Sat, 11 Aug 2012 21:48:54 +0000
changeset 379 7af3aaeb42e9
parent 291 61c3d984fb2d
child 413 362c5e77197d
permissions -rw-r--r--
Added a few checks and delays to the driver loading code to increase the chance of loading the driver.
     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-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.IO;
    13 using System.Text;
    14 using System.Threading;
    15 using System.Windows.Forms;
    16 using OpenHardwareMonitor.GUI;
    17 
    18 namespace OpenHardwareMonitor {
    19   public static class Program {
    20 
    21     [STAThread]
    22     public static void Main() {
    23       #if !DEBUG
    24         Application.ThreadException += 
    25           new ThreadExceptionEventHandler(Application_ThreadException);
    26         Application.SetUnhandledExceptionMode(
    27           UnhandledExceptionMode.CatchException);
    28 
    29         AppDomain.CurrentDomain.UnhandledException += 
    30           new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    31       #endif
    32 
    33       if (!AllRequiredFilesAvailable())
    34         Environment.Exit(0);
    35 
    36       Application.EnableVisualStyles();
    37       Application.SetCompatibleTextRenderingDefault(false);
    38       using (GUI.MainForm form = new GUI.MainForm()) {
    39         form.FormClosed += delegate(Object sender, FormClosedEventArgs e) {
    40           Application.Exit();
    41         };        
    42         Application.Run();
    43       }
    44     }
    45 
    46     private static bool IsFileAvailable(string fileName) {
    47       string path = Path.GetDirectoryName(Application.ExecutablePath) +
    48         Path.DirectorySeparatorChar;
    49 
    50       if (!File.Exists(path + fileName)) {
    51         MessageBox.Show("The following file could not be found: " + fileName + 
    52           "\nPlease extract all files from the archive.", "Error",
    53            MessageBoxButtons.OK, MessageBoxIcon.Error);
    54         return false;
    55       }
    56       return true;      
    57     }
    58 
    59     private static bool AllRequiredFilesAvailable() {
    60       if (!IsFileAvailable("Aga.Controls.dll"))
    61         return false;
    62       if (!IsFileAvailable("OpenHardwareMonitorLib.dll"))
    63         return false;
    64 
    65       return true;
    66     }
    67 
    68     private static void ReportException(Exception e) {
    69       CrashForm form = new CrashForm();
    70       form.Exception = e;
    71       form.ShowDialog();
    72     }
    73 
    74     public static void Application_ThreadException(object sender, 
    75       ThreadExceptionEventArgs e) 
    76     {
    77       try {
    78         ReportException(e.Exception);
    79       } catch {
    80       } finally {
    81         Application.Exit();
    82       }
    83     }
    84 
    85     public static void CurrentDomain_UnhandledException(object sender, 
    86       UnhandledExceptionEventArgs args) 
    87     {
    88       try {
    89         Exception e = args.ExceptionObject as Exception;
    90         if (e != null)
    91           ReportException(e);
    92       } catch {
    93       } finally {
    94         Environment.Exit(0);
    95       }
    96     }   
    97   }
    98 }