Program.cs (2213B)
1 using System; 2 using System.Diagnostics; 3 using System.Threading; 4 using System.Windows.Forms; 5 using LanTool.Classes; 6 7 [assembly: CLSCompliant(true)] 8 namespace LanTool 9 { 10 static class Program 11 { 12 public static MainForm MainWindow; 13 14 /// <summary> 15 /// Länger andauernde Funktion im Hintergrund starten (damit Oberfläche bedienbar bleibt) 16 /// </summary> 17 /// <param name="obj">Owner</param> 18 /// <param name="procname">Name der Funktion die aufzurufen ist</param> 19 /// <param name="para">Parameter des Aufrufs</param> 20 public static void RunBackgroundTask( object obj, string procname, object para ) 21 { 22 try 23 { 24 WaitCallback wc = ( WaitCallback )Delegate.CreateDelegate( typeof( WaitCallback ), obj, procname ); 25 if ( para == null ) 26 { 27 ThreadPool.QueueUserWorkItem( wc ); 28 } 29 else 30 { 31 ThreadPool.QueueUserWorkItem( wc, para ); 32 } 33 } 34 catch ( Exception ex ) 35 { 36 Console.WriteLine( ex.ToString() ); 37 } 38 } 39 40 /// <summary> 41 /// The main entry point for the application. 42 /// </summary> 43 [STAThread] 44 static void Main() 45 { 46 Application.EnableVisualStyles(); 47 Application.SetCompatibleTextRenderingDefault( false ); 48 49 Boolean createdNew; 50 Mutex mut = new Mutex( true, "LanTool_" + Application.StartupPath.Replace( @"\", "_" ), out createdNew ); 51 try 52 { 53 if ( createdNew ) 54 { 55 MainWindow = new MainForm(); 56 Application.Run( MainWindow ); 57 58 // Release the Mutex. 59 mut.ReleaseMutex(); 60 } 61 else 62 { 63 // Alle Processe die "LanTool" heißen suchen 64 Process[] ltprocs = Process.GetProcessesByName( "LanTool" ); 65 66 // Prozesse in den Vordergrund holen 67 foreach ( Process ltproc in ltprocs ) 68 { 69 if ( ltproc.MainModule.FileName.Equals( Application.ExecutablePath, 70 StringComparison.OrdinalIgnoreCase ) && 71 ltproc.Id != Process.GetCurrentProcess().Id ) 72 { 73 NativeMethods.SetForegroundWindow( ( int )ltproc.MainWindowHandle ); 74 break; 75 } 76 } 77 } 78 } 79 catch ( Exception ex ) 80 { 81 MessageBox.Show( ex.ToString() ); 82 } 83 finally 84 { 85 // Mutex schließen 86 if ( mut != null ) 87 { 88 if ( createdNew ) 89 { 90 mut.Close(); 91 } 92 } 93 } 94 } 95 } 96 }