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 { }
        }
    }
}

 

posted @ 2015-11-04 16:39  天翊无影  阅读(1581)  评论(0编辑  收藏  举报