Exemplo n.º 1
0
        private static void Main()
        {
            bool createdNew;

            InitializeLogger();
            Log.Information("Application Starts");
            var audioDeviceLister = new AudioDeviceLister(DeviceState.Active);

            using (var recordingDevices = audioDeviceLister.GetRecordingDevices())
            {
                Log.Information("Devices Recording {device}", recordingDevices);
            }

            using (var playbackDevices = audioDeviceLister.GetPlaybackDevices())
            {
                Log.Information("Devices Playback {device}", playbackDevices);
            }
#if !DEBUG
            AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
            {
                HandleException((Exception)args.ExceptionObject);
            };

            Log.Information("Set Exception Handler");
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
            WindowsAPIAdapter.Start(Application_ThreadException);
#else
            WindowsAPIAdapter.Start();
#endif
            Thread.CurrentThread.CurrentUICulture = LanguageParser.ParseLanguage(AppModel.Instance.Language);

            using (new Mutex(true, Application.ProductName, out createdNew))
            {
                if (!createdNew)
                {
                    Log.Warning("Application already started");
                    using (var client = new IPCClient(AppConfigs.IPCConfiguration.ClientUrl()))
                    {
                        try
                        {
                            var service = client.GetService();
                            service.StopApplication();
                            RestartApp();
                            return;
                        }
                        catch (RemotingException e)
                        {
                            Log.Error(e, "Unable to stop another running application");
                            Application.Exit();
                            return;
                        }
                    }
                }

                AppModel.Instance.ActiveAudioDeviceLister = new AudioDeviceLister(DeviceState.Active);

                // Windows Vista or newer.
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    SetProcessDPIAware();
                }

                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) =>
                {
                    Log.Debug("Restart Event received: {Event}", @event);
                    switch (@event.Type)
                    {
                    case WindowsAPIAdapter.RestartManagerEventType.Query:
                        @event.Result = new IntPtr(1);

                        break;

                    case WindowsAPIAdapter.RestartManagerEventType.EndSession:
                    case WindowsAPIAdapter.RestartManagerEventType.ForceClose:
                        Log.Debug("Close Application");
                        Application.Exit();
                        break;
                    }
                };

                Log.Information("Set Tray Icon with Main");
#if !DEBUG
                try
                {
#endif
                MMNotificationClient.Instance.Register();
                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.TrayIcon = icon;
                        AppModel.Instance.InitializeMain();
                        AppModel.Instance.NewVersionReleased += (sender, @event) =>
                        {
                            if (@event.UpdateMode == UpdateMode.Silent)
                            {
                                new AutoUpdater("/VERYSILENT /NOCANCEL /NORESTART", ApplicationPath.Default).Update(@event.Release, true);
                            }
                        };
                        if (AppConfigs.Configuration.FirstRun)
                        {
                            icon.ShowSettings();
                            AppConfigs.Configuration.FirstRun = false;
                            Log.Information("First run");
                        }
                        Application.Run();
                    }
#if !DEBUG
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
#endif
            }
            MMNotificationClient.Instance.UnRegister();
            WindowsAPIAdapter.Stop();
            Log.CloseAndFlush();
        }
Exemplo n.º 2
0
        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.TrayIcon = icon;
                        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();
        }