Example #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);
        }