static void Main() { bool createdNew; var mutexSecurity = new MutexSecurity(); mutexSecurity.AddAccessRule(new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow)); using (var setupMutex = new Mutex(false, @"Global\JosipMedved_Attache", out createdNew, mutexSecurity)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Medo.Application.UnhandledCatch.Attach(); Medo.Application.UnhandledCatch.ThreadException += new EventHandler <ThreadExceptionEventArgs>(UnhandledCatch_ThreadException); bool runInteractive = (Medo.Application.Args.Current.ContainsKey("Interactive")) || IsRunningOnMono; if (runInteractive) //start service threads { if (!IsRunningOnMono) { Tray.Show(); } AppServiceThread.Start(); Application.Run(); AppServiceThread.Stop(); if (!IsRunningOnMono) { Tray.Hide(); } } else if (Medo.Application.Args.Current.ContainsKey("Install")) { ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); Environment.Exit(0); } else if (Medo.Application.Args.Current.ContainsKey("Uninstall")) { try { ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); Environment.Exit(0); } catch (InstallException) { //no service with that name Environment.Exit(-1); } } else { if (Environment.UserInteractive) //show tray icon { Tray.Show(); Application.Run(); Tray.Hide(); } else { ServiceBase.Run(new ServiceBase[] { AppService.Instance }); } } } }
private static void Exit(int exitCode, string message) { if (Tray.IsVisible) { Tray.Hide(); } if (message == null) { message = "Exit due to an error."; } App.Log(EventLogEntryType.Error, message); AppService.Instance.ExitCode = exitCode; AppService.Instance.AutoLog = false; Environment.Exit(exitCode); }