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);
    }
相关推荐
陈陈爱java6 小时前
实习文档背诵
linux·服务器·windows
yngsqq8 小时前
cad c#二次开发 图层封装 获取当前层
java·数据库·c#
测试界8 小时前
部署Web UI自动化测试平台:SeleniumFlaskTester
windows·selenium·flask
隐-梵10 小时前
2025年测绘程序设计模拟赛一--地形图图幅编号及图廓点经纬度计算
windows·经验分享·visualstudio·c#
呉師傅11 小时前
奔图P2500NW打印机手机无线连接方法
运维·网络·windows·智能手机·计算机外设·电脑
lixy57911 小时前
wpf 动态转圈等待提示框
c#·wpf
南無忘码至尊12 小时前
Unity编辑器工具:一键为场景中所有MeshRenderer对象添加指定脚本
unity·c#·游戏引擎·游戏开发
海绵宝宝汉堡包12 小时前
未给任务“Fody.WeavingTask”的必需参数“IntermediateDir”赋值。 WpfTreeView
c#
枯萎穿心攻击13 小时前
算法入门第一篇:算法核心:复杂度分析与数组基础
算法·unity·矩阵·c#·游戏引擎
许野平14 小时前
Rust:如何开发32位的DLL动态库
windows·rust·动态库·dll·32位