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

一、保证只有一个进程

1.1 关闭旧的,打开新的

csharp 复制代码
   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 程序打开后不再打开新程序

csharp 复制代码
 protected override void OnStartup(StartupEventArgs e) {
            base.OnStartup(e);
            var process =Process.GetProcessesByName("Dog");
            if (process.Count() > 1) {
                MessageBox.Show("已经打开一个程序");
                Process.GetCurrentProcess().Kill();
            }
        }

二、异常捕捉

csharp 复制代码
   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);
    }
相关推荐
hixiong1231 小时前
C# OpenCVSharp使用 读光-票证检测矫正模型
人工智能·opencv·c#
霜绛1 小时前
C#知识补充(二)——命名空间、泛型、委托和事件
开发语言·学习·unity·c#
好望角雾眠2 小时前
第四阶段C#通讯开发-6:Socket之UDP
开发语言·笔记·学习·udp·c#
淮北4943 小时前
windows11配置wsl安装ubuntu20.04
windows·学习·ubuntu·wsl
霜绛3 小时前
C#知识补充(一)——ref和out、成员属性、万物之父和装箱拆箱、抽象类和抽象方法、接口
开发语言·笔记·学习·c#
爱编程的鱼3 小时前
C# var 关键字详解:从入门到精通
开发语言·c#·solr
玩泥巴的5 小时前
解放双手!使用Roslyn生成代码让你的 HTTP 客户端开发变得如此简单
c#·.net·代码生成·roslyn
shykevin5 小时前
uni-app x开发商城系统,商品列表点击跳转至商品详情页
windows·uni-app
扶尔魔ocy5 小时前
【QT自定义2D控件】QGraphics绘制仪表盘
数据库·qt·microsoft
星释6 小时前
Rust 练习册 :Pig Latin与语言游戏
游戏·rust·c#