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);
    }
相关推荐
不坑老师14 分钟前
小工具显出大才能——不坑盒子为教育数字化转型贡献“新方案”
microsoft·word·excel·ppt·office
积跬步,慕至千里17 分钟前
AI平台Dataiku 支Windows系统安装过程总结
windows
FL162386312943 分钟前
[C++][cmake]基于C++在windows上部署yolo26的目标检测onnx模型
c++·windows·目标检测
hoiii1871 小时前
C# 俄罗斯方块游戏
开发语言·游戏·c#
chao1898442 小时前
C#实现OMRON FINS-TCP协议与PLC通信
网络·tcp/ip·c#
ytttr8732 小时前
基于C# WinForms实现多窗口通信
开发语言·microsoft·c#
fengfuyao9853 小时前
基于C# WinForm实现的串口调试助手源码
开发语言·c#
WellTung_6663 小时前
Windows opencode Desktop App配置 Azure GPT5.2和oh-my-opencode插件安装方法
windows·azure
weixin_421994784 小时前
认识数据 - 变量与数据类型
c#·.net·.netcore
Bruce_Liuxiaowei4 小时前
如何彻底禁用 Windows Defender(附安全模式终极方案)
windows·安全·网络安全·内网渗透