@@ -19,28 +19,35 @@ This Source Code Form is subject to the terms of the Mozilla Public
1919namespace OpenHardwareMonitor {
2020 public static class Program {
2121
22+ static Mutex mutex = new Mutex ( true , "{3661aef2-95dc-433e-932c-2e06112d24ec}" ) ;
23+
2224 [ STAThread ]
2325 public static void Main ( ) {
24- #if ! DEBUG
25- Application . ThreadException +=
26- new ThreadExceptionEventHandler ( Application_ThreadException ) ;
27- Application . SetUnhandledExceptionMode (
28- UnhandledExceptionMode . CatchException ) ;
29-
30- AppDomain . CurrentDomain . UnhandledException +=
31- new UnhandledExceptionEventHandler ( CurrentDomain_UnhandledException ) ;
32- #endif
3326
3427 if ( ! IsNetFramework45Installed ( ) )
3528 Environment . Exit ( 0 ) ;
3629
30+ if ( ! mutex . WaitOne ( TimeSpan . Zero , true ) ) {
31+ MessageBox . Show ( "Another instance of the application is already running." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
32+ Environment . Exit ( 0 ) ;
33+ }
34+
35+ #if ! DEBUG
36+ Application . ThreadException +=
37+ new ThreadExceptionEventHandler ( Application_ThreadException ) ;
38+ Application . SetUnhandledExceptionMode (
39+ UnhandledExceptionMode . CatchException ) ;
40+
41+ AppDomain . CurrentDomain . UnhandledException +=
42+ new UnhandledExceptionEventHandler ( CurrentDomain_UnhandledException ) ;
43+ #endif
44+
3745 Application . EnableVisualStyles ( ) ;
3846 Application . SetCompatibleTextRenderingDefault ( false ) ;
39- using ( GUI . MainForm form = new GUI . MainForm ( ) ) {
40- form . FormClosed += delegate ( Object sender , FormClosedEventArgs e ) {
41- Application . Exit ( ) ;
42- } ;
47+ using ( var form = new MainForm ( ) ) {
48+ form . FormClosed += ( sender , e ) => Application . Exit ( ) ;
4349 Application . Run ( ) ;
50+ mutex . ReleaseMutex ( ) ;
4451 }
4552 }
4653
0 commit comments