アプリケーションの多重起動の検知と、起動しているインスタンス間でのメッセージの送受信をサポートします。
Inheritance: IDisposable
Exemple #1
0
        /// <summary>
        /// 启动时
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
#if !DEBUG
            var appInstance = new MetroTrilithon.Desktop.ApplicationInstance().AddTo(this);
            if (appInstance.IsFirst)
#endif
            {
#if DEBUG
                if (e.Args.ContainsArg("-app"))
                {
                    this.ProcessCommandLineParameter(e.Args);
                    base.OnStartup(e);
                    return;
                }
                Logger.EnableTextLog = true;
#endif
                App.Current.ShutdownMode           = ShutdownMode.OnExplicitShutdown;
                this.DispatcherUnhandledException += App_DispatcherUnhandledException;
                DispatcherHelper.UIDispatcher      = this.Dispatcher;
                if (e.Args.ContainsArg("-log"))
                {
                    Logger.EnableTextLog = true;
                }
                SettingsHost.Load();
                this.compositeDisposable.Add(SettingsHost.Save);
                this.compositeDisposable.Add(ProxyService.Current.Shutdown);
                this.compositeDisposable.Add(SteamConnectService.Current.Shutdown);

                GeneralSettings.Culture.Subscribe(x => ResourceService.Current.ChangeCulture(x)).AddTo(this);
                WindowService.Current.AddTo(this).Initialize();
                ProxyService.Current.Initialize();
                SteamConnectService.Current.Initialize();
                AuthService.Current.Initialize();
                if (GeneralSettings.IsAutoCheckUpdate)
                {
                    AutoUpdateService.Current.CheckUpdate();
                }

                //托盘加载
                TaskbarService.Current.Taskbar = (TaskbarIcon)FindResource("Taskbar");
                ThemeService.Current.Register(this, Theme.Windows, Accent.Windows);

                SetWebBrowserIeVersion();

                this.MainWindow = WindowService.Current.GetMainWindow();
                if (e.Args.ContainsArg("-minimized") || GeneralSettings.IsStartupAppMinimized.Value)
                {
                    //this.MainWindow.Show();
                    //(WindowService.Current.MainWindow as MainWindowViewModel).IsVisible = false;
                    (WindowService.Current.MainWindow as MainWindowViewModel).Initialize();
                }
                else
                {
                    this.MainWindow.Show();
                }

#if !DEBUG
                appInstance.CommandLineArgsReceived += (sender, args) =>
                {
                    // 检测到多次启动时将主窗口置于最前面
                    this.Dispatcher.Invoke(() => { (WindowService.Current.MainWindow as MainWindowViewModel).IsVisible = true; });
                    this.ProcessCommandLineParameter(args.CommandLineArgs);
                };
#endif

                base.OnStartup(e);
            }
#if !DEBUG
            else
            {
                if (e.Args.Length > 0)
                {
                    this.ProcessCommandLineParameter(e.Args);
                }
                else
                {
                    appInstance.SendCommandLineArgs(e.Args);
                }
            }
#endif
        }
Exemple #2
0
        /// <summary>
        /// 启动时
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            //AppCenter.Start("ccca922e-40fe-48ab-9982-45ba496b1201",
            //typeof(Analytics), typeof(Crashes));
#if !DEBUG
            var appInstance = new MetroTrilithon.Desktop.ApplicationInstance().AddTo(this);
            if (appInstance.IsFirst)
#endif
            {
#if DEBUG
                if (e.Args.ContainsArg("-app"))
                {
                    this.ProcessCommandLineParameter(e.Args);
                    base.OnStartup(e);
                    return;
                }
                Logger.EnableTextLog = true;
#endif
                App.Current.ShutdownMode           = ShutdownMode.OnExplicitShutdown;
                this.DispatcherUnhandledException += App_DispatcherUnhandledException;
                DispatcherHelper.UIDispatcher      = this.Dispatcher;
                SettingsHost.Load();
                this.compositeDisposable.Add(SettingsHost.Save);
                this.compositeDisposable.Add(ProxyService.Current.Shutdown);
                this.compositeDisposable.Add(SteamConnectService.Current.Shutdown);
                this.compositeDisposable.Add(() =>
                {
                    if (TaskbarService.Current.Taskbar != null)
                    {
                        //TaskbarService.Current.Taskbar.Icon = null; //避免托盘图标没有自动消失
                        TaskbarService.Current.Taskbar.Icon.Dispose();
                    }
                });

                Microsoft.Win32.SystemEvents.SessionEnding += SystemEvents_SessionEnding;

                if (e.Args.ContainsArg("-log") || GeneralSettings.IsEnableLogRecord)
                {
                    Logger.EnableTextLog = true;
                }
                //每次启动都覆盖设置一次开机自启确保路径变换后开机自启依然生效
                if (GeneralSettings.WindowsStartupAutoRun)
                {
                    var steamService = SteamToolCore.Instance.Get <SteamToolService>();
                    steamService.SetWindowsStartupAutoRun(GeneralSettings.WindowsStartupAutoRun.Value, ProductInfo.Title);
                }


                GeneralSettings.Culture.Subscribe(x => ResourceService.Current.ChangeCulture(x)).AddTo(this);
                WindowService.Current.AddTo(this).Initialize();
                ProxyService.Current.Initialize();
                SteamConnectService.Current.Initialize();
                AuthService.Current.Initialize();
                if (GeneralSettings.IsAutoCheckUpdate.Value)
                {
                    AutoUpdateService.Current.CheckUpdate();
                }

                //托盘加载
                TaskbarService.Current.Taskbar = (TaskbarIcon)FindResource("Taskbar");
                ThemeService.Current.Register(this, Theme.Windows, Accent.Windows);

                SetWebBrowserIeVersion();

                this.MainWindow = WindowService.Current.GetMainWindow();
                if (e.Args.ContainsArg("-minimized") || GeneralSettings.IsStartupAppMinimized.Value)
                {
                    //this.MainWindow.Show();
                    //(WindowService.Current.MainWindow as MainWindowViewModel).IsVisible = false;
                    (WindowService.Current.MainWindow as MainWindowViewModel).Initialize();
                }
                else
                {
                    this.MainWindow.Show();
                }

                if (GeneralSettings.IsAutoRunSteam.Value && Process.GetProcessesByName("steam").Length < 1)
                {
                    var steamTool = SteamToolCore.Instance.Get <SteamToolService>();
                    if (!string.IsNullOrEmpty(steamTool.SteamPath))
                    {
                        steamTool.StartSteam("-silent " + GeneralSettings.SteamStratParameter.Value);
                    }
                }

#if !DEBUG
                appInstance.CommandLineArgsReceived += (sender, args) =>
                {
                    // 检测到多次启动时将主窗口置于最前面
                    this.Dispatcher.Invoke(() =>
                    {
                        (WindowService.Current.MainWindow as MainWindowViewModel).IsVisible = true;
                    });
                    //this.ProcessCommandLineParameter(args.CommandLineArgs);
                };
#endif

                base.OnStartup(e);
            }
#if !DEBUG
            else
            {
                if (e.Args.Length > 0)
                {
                    this.ProcessCommandLineParameter(e.Args);
                }
                else
                {
                    appInstance.SendCommandLineArgs(e.Args);
                    App.Current.Shutdown();
                }
            }
#endif
        }