예제 #1
0
        //private void ShowException(Exception ex,int type)
        //{
        //    try
        //    {
        //        Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("程序发生了未捕获的错误,类型"+type.ToString(), ex));

        //        File.AppendAllText("Exception.log", Environment.NewLine + Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + ex.ToString());
        //    }
        //    catch (Exception ex2)
        //    {
        //        Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("错误信息无法写入", ex2));
        //    }
        //    finally
        //    {
        //        App.Current.Shutdown();
        //    }
        //}

        private void ApplicationStartupEventHandler(object sender, StartupEventArgs e)
        {
            ConcernConfigPath();
            Load();
            if (e.Args.Length > 0)
            {
                if (e.Args[0] == "noWindow")
                {
                    startup = true;
                }
                else if (e.Args[0] == "readonly")
                {
                    ReadOnlyMode = true;
                }
            }

            if (!ReadOnlyMode)
            {
#if !DEBUG
                if (WpfCodes.Program.Startup.HaveAnotherInstance("ComputerUsage"))
                {
                    WpfControls.Dialog.DialogHelper.ShowError("已存在另一实例,请不要重复运行!");
                    Environment.Exit(0);
                    return;
                }

                //TaskScheduler.UnobservedTaskException += (p1, p2) => { if (!p2.Observed) ShowException(p2.Exception,3); };//Task
                //AppDomain.CurrentDomain.UnhandledException += (p1, p2) => ShowException((Exception)p2.ExceptionObject,2);//UI
                //DispatcherUnhandledException += (p1, p2) => ShowException(p2.Exception,1);//Thread
                new WpfCodes.Program.Exception().UnhandledException += (p1, p2) =>
                {
                    try
                    {
                        Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("程序发生了未捕获的错误,类型" + p2.Source.ToString(), p2.Exception));

                        File.AppendAllText("Exception.log", Environment.NewLine + Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + p2.Exception.ToString());
                    }
                    catch (Exception ex)
                    {
                        Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("错误信息无法写入", ex));
                    }
                    finally
                    {
                        App.Current.Shutdown();
                    }
                };
#endif
                background = new BackgroundWork();

                Dictionary <string, Action> rightMouseClick = new Dictionary <string, Action>
                {
                    { "回收", () => GC.Collect() },

                    { "退出", () => Shutdown() },
                };
                trayIcon = new WpfCodes.Program.TrayIcon(ComputerUsage.Properties.Resources.ICON, "计算机使用情况", newWindow, rightMouseClick);
                trayIcon.Show();
                Task.Run(() =>
                {
                    ComputerDatas.RegistSystemEvents();
                });
                ClipboardHelper clipBoardHelper = new ClipboardHelper();


                ProcessMonitorHelper.Load();
            }

            if (!startup)
            {
                newWindow();
            }
            void newWindow()
            {
                if (Current.MainWindow as MainWindow == null)
                {
                    Current.MainWindow = new MainWindow();
                    Current.MainWindow.Show();
                }
            };
        }