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

wpf 异常处理和关闭进程的更多相关文章

  1. Dos命令查看端口占用及关闭进程

    1. 查看端口占用 在windows命令行窗口下执行: netstat -aon|findstr "8080" TCP 127.0.0.1:80 0.0.0.0:0 LISTENI ...

  2. Centos下查看占用端口并关闭进程方法

    1.查看端口占用情况:netstat –tlnp   (加p可以看到是哪个进程占用了端口); 也可以用grep查找对应的被占用的端口,键入netstat –tlnp | grep 3306可以看到PI ...

  3. innosetup安装之前关闭进程

    InnoSetup覆盖安装的时候可能会因为源程序正在运行而安装失败,以下脚本能够关闭原运行进程. [code] // 安装前检查关闭**进程 function InitializeSetup():Bo ...

  4. hadoop 关闭进程时报错no 进程 to stop

    前两天和朋友李天王吃饭的时候,聊到了一个hadoop的运维的很简单问题,感觉很有意思,以前也没有注意过,现在加以重现和整理.   感谢李天王的分享....   翻看了yarn-deamon.sh st ...

  5. 在CMD命令行下关闭进程的命令

    转载: [重要]在CMD命令行下关闭进程的命令━━━━━━━━━━━━━━━━━━━━━━━━━━ 方法一: 在"运行"中输入:ntsd -c q -pn 程序名字(在MS-Dos ...

  6. php实现多进程和关闭进程

    一.php实现多进程 PHP有个pcntl_fork的函数可以实现多进程,但要加载pcntl拓展,而且只有在linux下才能编译这个拓展. 先代码: <?php$arr = ['30000000 ...

  7. C# 关闭进程的时候总是捕捉到System.Threading.ThreadAbortException: 正在中止线程

    C# 关闭进程的时候总是捕捉到System.Threading.ThreadAbortException: 正在中止线程 这是由ThreadAbortException抛出的 可以写成下面的样子 tr ...

  8. C#利用ntsd.exe关闭进程

    有时我们会发现在任务管理器中无法关闭某个进程,可以使用下面的方法强行关闭,注意不要杀掉进程表中的系统核心进程: 1.使用Windows XP/2000自带的工具 从Windows 2000开始,Win ...

  9. Winfrom 桌面弹窗拦截 关闭进程简易程序 源代码下载

    ***********************2019 2.7更新 v 2.0*************************************************** 程序 源代码 交互 ...

随机推荐

  1. ArrayList、Vector、HashMap、HashSet的默认初始容量、加载因子、扩容增量

    当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全部复制到新的内存上,这无疑使效率大大降低. 加载因 ...

  2. 鹏程网用户管理系统学习(2016-07-18 by 徐鹏)

    新的平台:X平台(x.hna.net)旧的平台:鹏程网(www.hna.net) 如今的平台情况:很多业务已经转到X平台,但也存在少量的业务还是用鹏程网的旧系统.例如**用户管理系统(也逐渐转移到易服 ...

  3. .NET和JAVA中BYTE的区别以及JAVA中“DES/CBC/PKCS5PADDING” 加密解密在.NET中的实现

    场景:java 作为客户端调用已有的一个.net写的server的webservice,输入string,返回字节数组. 问题:返回的值不是自己想要的,跟.net客户端直接调用总是有差距 分析:平台不 ...

  4. [总结] Versions crashing in OS X Yosemite (10.10)

    在文本编辑器中打开 ~/.subversion/servers 在 [global] 下添加该行: http-library = serf 然后,安装 Versions 1.3.2

  5. HTTP状态码(HTTP Status Code)

    一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说 ...

  6. 初识SpringMvc

    初识SpringMvc springMvc简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 s ...

  7. iOS开发中的这些权限,你搞懂了吗?

    APP开发避免不开系统权限的问题,如何在APP以更加友好的方式向用户展示系统权限,似乎也是开发过程中值得深思的一件事. 那如何提高APP获取iOS系统权限的通过率呢?有以下几种方式:1.在用户打开AP ...

  8. url中的特殊符号含义

    1. # 10年9月,twitter改版.一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为http://twitter.com/username改版后,就变 ...

  9. docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速

    一.前言 先回顾下上一节创建docker-machine的过程,默认情况下docker toolbox中的docker-machine使用virtual box创建虚拟机,KI首次启动时创建虚拟机的过 ...

  10. 编写轻量ajax组件03-实现(附源码)

    前言 通过前两篇的介绍,我们知道要执行页面对象的方法,核心就是反射,是从请求获取参数并执行指定方法的过程.实际上这和asp.net mvc框架的核心思想很类似,它会解析url,从中获取controll ...