• C# App.xaml.cs的一些操作


    一、保证只有一个进程

    1.1 关闭旧的,打开新的

       protected override void OnStartup(StartupEventArgs e) {
                base.OnStartup(e);
                var process =Process.GetProcessesByName("Dog");
                if (process.Count() > 1) {
                    var list = process.ToList();
                    list.Sort((p1,p2)=>p1.StartTime.CompareTo(p2.StartTime));
                    list[0].Kill();
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1.2 程序打开后不再打开新程序

     protected override void OnStartup(StartupEventArgs e) {
                base.OnStartup(e);
                var process =Process.GetProcessesByName("Dog");
                if (process.Count() > 1) {
                    MessageBox.Show("已经打开一个程序");
                    Process.GetCurrentProcess().Kill();
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    二、异常捕捉

       public App()
            {
            //注册全局事件
               AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    
                DispatcherUnhandledException += App_DispatcherUnhandledException;
    
                TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
    
            }
    
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
        {
            const string msg = "主线程异常";
            try
            {
                if (args.ExceptionObject is Exception && Dispatcher != null)
                {
                    Dispatcher.Invoke(() =>
                    {
                        Exception ex = (Exception)args.ExceptionObject;
                        HandleException(msg, ex);
                    });
                }
            }
            catch (Exception ex)
            {
                HandleException(msg, ex);
            }
        }
    
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
        {
            const string msg = "子线程异常";
            try
            {
                HandleException(msg, args.Exception);
                args.Handled = true;
            }
            catch (Exception ex)
            {
                HandleException(msg, ex);
            }
        }
    
        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs args)
        {
            const string msg = "异步异常";
            try
            {
                HandleException(msg, args.Exception);
                args.SetObserved();
            }
            catch (Exception ex)
            {
                HandleException(msg, ex);
            }
        }
    
        private void HandleException(string msg, Exception ex)
        {
    
                MessageBox.Show(ex.Message,msg);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
  • 相关阅读:
    操作指南|JumpServer堡垒机支持通过Passkey进行登录认证
    【Go语言】Go语言中的函数
    【ros】解决protobuf的安装问题
    Codeforces Round 901 (Div. 2)
    linux后台运行java项目/ jar包:nohup 命令
    知识图谱:语义网络、语义网、链接数据、知识图谱
    从零开始 Spring Boot 17:MyBatis Plus 续
    解决GPU显存句柄泄漏问题
    企业级软件定制开发的特点有哪些?
    Linux 可执行文件瘦身指令 strip 使用示例
  • 原文地址:https://blog.csdn.net/LyRics1996/article/details/133655030