private static void Main() { bool createdNew; AppLogger.Log.Info("Application Starts"); using (new Mutex(true, Application.ProductName, out createdNew)) { if (!createdNew) { AppLogger.Log.Warn("Application already started"); return; } AppModel.Instance.ActiveAudioDeviceLister = new AudioDeviceLister(DeviceState.Active); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); #if !DEBUG Application.ThreadException += Application_ThreadException; #endif WindowsAPIAdapter.Start(); //Manage the Closing events send by Windows //Since this app don't use a Form as "main window" the app doesn't close //when it should without this. WindowsAPIAdapter.RestartManagerTriggered += (sender, @event) => { using (AppLogger.Log.DebugCall()) { AppLogger.Log.Debug("Restart Event recieved", @event); switch (@event.Type) { case WindowsAPIAdapter.RestartManagerEventType.Query: @event.Result = new IntPtr(1); break; case WindowsAPIAdapter.RestartManagerEventType.EndSession: case WindowsAPIAdapter.RestartManagerEventType.ForceClose: AppLogger.Log.Debug("Close Application"); Application.Exit(); break; } } }; AppLogger.Log.Info("Set Exception Handler"); #if !DEBUG WindowsAPIAdapter.AddThreadExceptionHandler(Application_ThreadException); #endif AppLogger.Log.Info("Set Tray Icon with Main"); #if !DEBUG try { #endif using (var icon = new TrayIcon()) { if (AppConfigs.Configuration.FirstRun) { icon.ShowSettings(); AppConfigs.Configuration.FirstRun = false; AppLogger.Log.Info("First run"); } Application.Run(); WindowsAPIAdapter.Stop(); } #if !DEBUG } catch (Exception ex) { HandleException(ex); } #endif } }
private static void Main() { bool createdNew; AppLogger.Log.Info("Application Starts"); #if !DEBUG AppDomain.CurrentDomain.UnhandledException += (sender, args) => { HandleException((Exception)args.ExceptionObject); }; AppLogger.Log.Info("Set Exception Handler"); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); WindowsAPIAdapter.Start(Application_ThreadException); #else WindowsAPIAdapter.Start(); #endif using (new Mutex(true, Application.ProductName, out createdNew)) { if (!createdNew) { AppLogger.Log.Warn("Application already started"); using (new AppLogger.LogRestartor()) { using (var client = new IPCClient(AppConfigs.IPCConfiguration.ClientUrl())) { try { var service = client.GetService(); service.StopApplication(); RestartApp(); return; } catch (RemotingException e) { AppLogger.Log.Error("Can't stop the other app ", e); Application.Exit(); return; } } } } AppModel.Instance.ActiveAudioDeviceLister = new AudioDeviceLister(DeviceState.Active); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Manage the Closing events send by Windows //Since this app don't use a Form as "main window" the app doesn't close //when it should without this. WindowsAPIAdapter.RestartManagerTriggered += (sender, @event) => { using (AppLogger.Log.DebugCall()) { AppLogger.Log.Debug("Restart Event recieved", @event); switch (@event.Type) { case WindowsAPIAdapter.RestartManagerEventType.Query: @event.Result = new IntPtr(1); break; case WindowsAPIAdapter.RestartManagerEventType.EndSession: case WindowsAPIAdapter.RestartManagerEventType.ForceClose: AppLogger.Log.Debug("Close Application"); Application.Exit(); break; } } }; AppLogger.Log.Info("Set Tray Icon with Main"); #if !DEBUG try { #endif using(var ipcServer = new IPCServer(AppConfigs.IPCConfiguration.ServerUrl())) using (var icon = new TrayIcon()) { var available = false; while (!available) { try { ipcServer.InitServer(); available = true; } catch (RemotingException) { Thread.Sleep(250); } } AppModel.Instance.NotifyIcon = icon.NotifyIcon; AppModel.Instance.InitializeMain(); AppModel.Instance.NewVersionReleased += (sender, @event) => { if (@event.UpdateState != UpdateState.Steath) { return; } new AutoUpdater("/VERYSILENT /NOCANCEL /NORESTART", ApplicationPath.Default).Update(@event.Release, true); }; if (AppConfigs.Configuration.FirstRun) { icon.ShowSettings(); AppConfigs.Configuration.FirstRun = false; AppLogger.Log.Info("First run"); } Application.Run(); } #if !DEBUG } catch (Exception ex) { HandleException(ex); } #endif } WindowsAPIAdapter.Stop(); }