• wpf 异常处理和关闭进程


    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Windows;
    using System.Diagnostics;
    using System.Data.SqlClient;
    
    namespace wpf
    {
        /// <summary>
        /// App.xaml 的交互逻辑
        /// </summary>
        public partial class App : Application
        {
            public static System.Threading.Mutex Run;
    
            protected override void OnStartup(StartupEventArgs e)
            {
                //注册事件
                Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
                base.OnStartup(e);
                bool noRun = false;
                try
                {
                    //判断是否在运行
                    Run = new System.Threading.Mutex(true, Process.GetCurrentProcess().ProcessName, out noRun);
                    if (noRun)
                    {
                        //获取进程名
                        Process[] localByName = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
                        Run.ReleaseMutex();
                        //项目中配置文件中的信息
                        string path = ConfigurationManager.ConnectionStrings["conn"].ToString();
                        //判断数据的连接
                        SqlConnection con = new SqlConnection(path);
                        try
                        {
                            con.Open();
                            if (con.State == ConnectionState.Open)
                            {
                                new Login().Show();
                            }
                            else
                            {
                                MessageBox.Show("数据库连接失败!", "信息提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                                Application.Current.Shutdown();
                            }
                        }
                        catch (Exception q)
                        {
                            MessageBox.Show("数据库连接失败!", "信息提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                            Application.Current.Shutdown();
                        }
    
                    }
                    else
                    {
                        MessageBox.Show("程序已在运行!", "信息提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        //Application.Current.Shutdown();
                     //  string proName=  Process.GetCurrentProcess().ProcessName;
                       
                    }
                }
                catch (Exception q)
                {
                    MessageBox.Show("数据库连接失败!", "信息提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                    Application.Current.Shutdown();
                }
            }
            /// <summary>
            /// 程序未知的异常的方法事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
            {
                MessageBox.Show("我们很抱歉,当前应用程序遇到一些问题,该操作已经终止,请重新启动.", "意外的操作", MessageBoxButton.OK, MessageBoxImage.Information);//这里通常需要给用户一些较为友好的提示,并且后续可能的操作
                Application.Current.Shutdown();
                //string proName = Process.GetCurrentProcess().ProcessName;
               // Process[] localByName = Process.GetProcessesByName(proName);//因为可以同时启动多
                e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
            }
            /// <summary>
            /// 根据进程名称进行关闭进程
            /// </summary>
            /// <param name="procesName"></param>
            public void killPro(string procesName)
            {
    
                try
                {
                    foreach (System.Diagnostics.Process Pro in System.Diagnostics.Process.GetProcessesByName(procesName))
                    {
                        if (!Pro.CloseMainWindow())
                        {
                            Pro.Kill();
                        }
                    }
    
                }
                catch { }
            }
        }
    }
  • 相关阅读:
    Shell学习笔记之shell脚本和python脚本实现批量ping IP测试
    SNMP学习笔记之SNMPv3的配置和认证以及TroubleShooting
    Web负载均衡学习笔记之四层和七层负载均衡的区别
    SNMP学习笔记之SNMP树形结构介绍
    Web负载均衡学习笔记之实现负载均衡的几种实现方式
    HCNP学习笔记之子网掩码的计算和划分详细
    HCNP学习笔记之IP地址、子网掩码、网关的关系
    Linux学习笔记之passwd:Authentication token manipulation error_错误的解决办法
    ubuntu 系统关键指令
    Jetson tk1 hash sum mismatch
  • 原文地址:https://www.cnblogs.com/tianyiwuying/p/4936430.html
Copyright © 2020-2023  润新知