コード例 #1
0
        // 添加启动参数的处理
        protected override void OnStartup(StartupEventArgs e)
        {
            // 根据传入参数进行任务调度
            var startupMode = StartupMode.Normal;

            if (e.Args.Length > 0)
            {
                System.Enum.TryParse(e.Args[0], out startupMode);
            }

            if (startupMode == StartupMode.Normal)
            {
                // 启动主窗体
                var mw = new MainWindow();
                Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
                Current.MainWindow   = mw;
                mw.Show();
                return;
            }
            if (startupMode == StartupMode.BackGround)
            {
                // 限制后台进程只能有一个
                var mutex = new System.Threading.Mutex(true, nameof(WorkOverTimeCounter), out var ret);
                if (!ret)
                {
                    //MessageBox.Show("已在运行中!");
                    Environment.Exit(0);
                }
                CounterHelper.StartCount();
                return;
            }

            // 设置/取消自启动后结束进程
            if (startupMode == StartupMode.SetSelfStart)
            {
                // 启动后台监视进程
                var mutex = new System.Threading.Mutex(true, nameof(WorkOverTimeCounter), out var ret);
                if (ret)
                {
                    var p           = new Process();
                    var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase);
                    processInfo.Arguments = StartupMode.BackGround.ToString();
                    p.StartInfo           = processInfo;
                    try
                    {
                        p.Start();
                    }
                    catch (Exception exce)
                    {
                        MessageBox.Show(exce.Message);
                    }
                }
                SetSelfStartingHelper.SetSelfStart();
            }
            if (startupMode == StartupMode.UnsetSelfStart)
            {
                SetSelfStartingHelper.UnsetSelfStart();
            }
            Environment.Exit(0);
        }
コード例 #2
0
        private void BtnDisReg_OnClick(object sender, RoutedEventArgs e)
        {
            if (AppElvatedHelper.IsElvated())
            {
                SetSelfStartingHelper.UnsetSelfStart();
            }
            else
            {
                AppElvatedHelper.RunElvatedTask(StartupMode.UnsetSelfStart);
            }
            var task = new Task(() =>
            {
                Thread.Sleep(500);
                CheckBackGround();
            });

            task.Start();
        }
コード例 #3
0
        private void App_OnStartup(object sender, StartupEventArgs startupEvent)
        {
            try
            {
                {
                    var appDateFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), SystemConfig.AppName);
                    if (!Directory.Exists(appDateFolder))
                    {
                        Directory.CreateDirectory(appDateFolder);
                    }
                    var logFilePath = Path.Combine(appDateFolder, "PRemoteM.log.md");
                    SimpleLogHelper.LogFileName = logFilePath;
                }

                #region single-instance app
                var startupMode = PRM.Core.Ulits.StartupMode.Normal;
                if (startupEvent.Args.Length > 0)
                {
                    System.Enum.TryParse(startupEvent.Args[0], out startupMode);
                }
                if (startupMode == PRM.Core.Ulits.StartupMode.SetSelfStart)
                {
                    SetSelfStartingHelper.SetSelfStart();
                    Environment.Exit(0);
                }
                if (startupMode == PRM.Core.Ulits.StartupMode.UnsetSelfStart)
                {
                    SetSelfStartingHelper.UnsetSelfStart();
                    Environment.Exit(0);
                }
                _singleAppMutex = new Mutex(true, PipeName, out var isFirst);
                if (!isFirst)
                {
                    try
                    {
                        var client = new NamedPipeClientStream(PipeName);
                        client.Connect();
                        StreamReader reader = new StreamReader(client);
                        StreamWriter writer = new StreamWriter(client);
                        writer.WriteLine("ActivateMe");
                        writer.Flush();
                        client.Dispose();
                    }
                    catch (Exception e)
                    {
                        SimpleLogHelper.Warning(e);
                    }

                    Environment.Exit(0);
                }
                else
                {
                    Task.Factory.StartNew(() =>
                    {
                        NamedPipeServerStream server = null;
                        while (true)
                        {
                            server?.Dispose();
                            server = new NamedPipeServerStream(PipeName);
                            SimpleLogHelper.Debug("NamedPipeServerStream.WaitForConnection");
                            server.WaitForConnection();

                            try
                            {
                                var reader = new StreamReader(server);
                                var line   = reader.ReadLine();
                                if (!string.IsNullOrEmpty(line))
                                {
                                    SimpleLogHelper.Debug("NamedPipeServerStream get: " + line);
                                    if (line == "ActivateMe")
                                    {
                                        if (App.Window != null)
                                        {
                                            Dispatcher.Invoke(() =>
                                            {
                                                if (App.Window.WindowState == WindowState.Minimized)
                                                {
                                                    App.Window.WindowState = WindowState.Normal;
                                                }
                                                App.Window.ActivateMe();
                                            });
                                        }
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                SimpleLogHelper.Warning(e);
                            }
                        }
                    });
                }
                #endregion


#if DEBUG
                Shawn.Ulits.ConsoleManager.Show();
#endif

                #region system check & init


                #region Init
                {
                    var appDateFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), SystemConfig.AppName);
                    if (!Directory.Exists(appDateFolder))
                    {
                        Directory.CreateDirectory(appDateFolder);
                    }
                    SimpleLogHelper.LogFileName = Path.Combine(appDateFolder, "PRemoteM.log.md");
                    var iniPath = Path.Combine(appDateFolder, SystemConfig.AppName + ".ini");
                    if (Environment.CurrentDirectory.IndexOf(@"C:\Windows") < 0)
                    {
                        if (File.Exists(SystemConfig.AppName + ".ini") ||
                            IOPermissionHelper.HasWritePermissionOnDir("./"))
                        {
                            iniPath = SystemConfig.AppName + ".ini";
                        }
                    }
                    var ini = new Ini(iniPath);
                    //if (!File.Exists(iniPath))
                    //{
                    //    // TODO if ini is not existed, then it would be a new user, open guide to set db path
                    //}

                    // Set default folder path
                    SystemConfigLanguage.LanguageJsonDir = Path.Combine(appDateFolder, SystemConfigLanguage.LanguageJsonDir);
                    PuttyColorThemes.ThemeRegFileFolder  = Path.Combine(appDateFolder, PuttyColorThemes.ThemeRegFileFolder);

                    var language     = new SystemConfigLanguage(this.Resources, ini);
                    var general      = new SystemConfigGeneral(ini);
                    var quickConnect = new SystemConfigQuickConnect(ini);
                    var theme        = new SystemConfigTheme(this.Resources, ini);
                    var dataSecurity = new SystemConfigDataSecurity(ini);


                    //if (!File.Exists(dataSecurity.DbPath))
                    //{
                    //    // TODO db is not existed, then tell our user to create new one or select a new one.
                    //}

                    // config create instance (settings & langs)
                    SystemConfig.Init();
                    SystemConfig.Instance.General      = general;
                    SystemConfig.Instance.Language     = language;
                    SystemConfig.Instance.QuickConnect = quickConnect;
                    SystemConfig.Instance.DataSecurity = dataSecurity;
                    SystemConfig.Instance.Theme        = theme;

                    // server data holder init.
                    GlobalData.Init();

                    // remote window pool init.
                    RemoteWindowPool.Init();
                }
                #endregion

                // kill putty process
                foreach (var process in Process.GetProcessesByName(PuttyHost.PuttyExeName.ToLower().Replace(".exe", "")))
                {
                    try
                    {
                        process.Kill();
                    }
                    catch
                    {
                    }
                }



                #endregion


                #region app start
                // main window init
                {
                    Window         = new MainWindow();
                    ShutdownMode   = ShutdownMode.OnMainWindowClose;
                    MainWindow     = Window;
                    Window.Closed += (o, args) => { AppOnClose(); };
                    if (!SystemConfig.Instance.General.AppStartMinimized)
                    {
                        ActivateWindow();
                    }

                    // check if Db is ok
                    var res = SystemConfig.Instance.DataSecurity.CheckIfDbIsOk();
                    if (!res.Item1)
                    {
                        SimpleLogHelper.Info("Start with 'SystemConfigPage' by 'ErroFlag'.");
                        MessageBox.Show(res.Item2, SystemConfig.Instance.Language.GetText("messagebox_title_error"));
                        ActivateWindow();
                        Window.VmMain.CmdGoSysOptionsPage.Execute(typeof(SystemConfigDataSecurity));
                    }
                    else
                    {
                        // load data
                        GlobalData.Instance.ServerListUpdate();
                    }
                }


                // task tray init
                InitTaskTray();


                // quick search init
                InitQuickSearch();
                #endregion
            }
            catch (Exception ex)
            {
                SimpleLogHelper.Fatal(ex.Message, ex.StackTrace);
#if DEBUG
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
#endif
                AppOnClose(-1);
            }
        }