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);
    }
相关推荐
找死的豆腐7 小时前
基于.NET的Windows窗体编程之WinForms图像控件
windows·.net
XUHUOJUN7 小时前
Azure Local GPU 部署与企业应用场景指南(上篇 )
windows·microsoft·架构·nvidia·azure local
临风细雨8 小时前
从 Bun 的 Rust 重写,看 C# 如何重建 AI 基础设施层
人工智能·rust·c#
吴可可1238 小时前
C# CAD二次开发自定义实体实现
c#
AOwhisky9 小时前
Python 基础语法(上篇 + 下篇)——综合自测题
linux·windows·python
z落落10 小时前
C# WinForm 线程池与事件等待+进度条暂停恢复实战案例
开发语言·c#
L歪歪君10 小时前
开源大模型本地部署硬件选型深度指南
microsoft
埃博拉酱10 小时前
Pip/Conda 混用导致的 CRT 版本冲突问题:[WinError 1114] 动态链接库(DLL)初始化例程失败
windows·python
CoderYanger11 小时前
视频切割脚本(Python版)
linux·开发语言·windows·后端·python·程序人生·职场和发展
西游音月11 小时前
Windows环境下的WSL+Claude Code安装配置
windows·大模型·ai编程·wsl·claude code