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);
    }
相关推荐
Lv_沐曦1 小时前
Windows磁盘驱动器取消黄色感叹号的方法
运维·windows·bitlocker·黄色感叹号
jiayong232 小时前
海量数据处理技术方案与实现原理
大数据·c#·linq
J2虾虾2 小时前
Spring AI Alibaba - Models 模型
人工智能·spring·microsoft
ITHAOGE153 小时前
下载 | Windows Server 2022官方原版ISO映像!(5月更新、标准版、数据中心版、20348.5139)
windows·科技·微软·电脑
YJlio3 小时前
CSDN AI数字营销实测体验:多平台账号一键分发到底好不好用?我做了一次完整实测
人工智能·windows·企业微信·火绒安全·系统备份·easyimagex
零号全栈寒江独钓3 小时前
c++跨平台实现日志重定向
linux·c++·windows
Crazy learner4 小时前
Windows 微软商店更新失败(错误代码 0x80004002)完整解决指南(2026最新有效)
windows·microsoft
Arenaschi5 小时前
关于GPT的版特点
java·网络·人工智能·windows·python·gpt
阿昭L5 小时前
Windows认证和安全对象的基本概念
windows·安全