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 { }
}
}
}
wpf 异常处理和关闭进程的更多相关文章
- Dos命令查看端口占用及关闭进程
1. 查看端口占用 在windows命令行窗口下执行: netstat -aon|findstr "8080" TCP 127.0.0.1:80 0.0.0.0:0 LISTENI ...
- Centos下查看占用端口并关闭进程方法
1.查看端口占用情况:netstat –tlnp (加p可以看到是哪个进程占用了端口); 也可以用grep查找对应的被占用的端口,键入netstat –tlnp | grep 3306可以看到PI ...
- innosetup安装之前关闭进程
InnoSetup覆盖安装的时候可能会因为源程序正在运行而安装失败,以下脚本能够关闭原运行进程. [code] // 安装前检查关闭**进程 function InitializeSetup():Bo ...
- hadoop 关闭进程时报错no 进程 to stop
前两天和朋友李天王吃饭的时候,聊到了一个hadoop的运维的很简单问题,感觉很有意思,以前也没有注意过,现在加以重现和整理. 感谢李天王的分享.... 翻看了yarn-deamon.sh st ...
- 在CMD命令行下关闭进程的命令
转载: [重要]在CMD命令行下关闭进程的命令━━━━━━━━━━━━━━━━━━━━━━━━━━ 方法一: 在"运行"中输入:ntsd -c q -pn 程序名字(在MS-Dos ...
- php实现多进程和关闭进程
一.php实现多进程 PHP有个pcntl_fork的函数可以实现多进程,但要加载pcntl拓展,而且只有在linux下才能编译这个拓展. 先代码: <?php$arr = ['30000000 ...
- C# 关闭进程的时候总是捕捉到System.Threading.ThreadAbortException: 正在中止线程
C# 关闭进程的时候总是捕捉到System.Threading.ThreadAbortException: 正在中止线程 这是由ThreadAbortException抛出的 可以写成下面的样子 tr ...
- C#利用ntsd.exe关闭进程
有时我们会发现在任务管理器中无法关闭某个进程,可以使用下面的方法强行关闭,注意不要杀掉进程表中的系统核心进程: 1.使用Windows XP/2000自带的工具 从Windows 2000开始,Win ...
- Winfrom 桌面弹窗拦截 关闭进程简易程序 源代码下载
***********************2019 2.7更新 v 2.0*************************************************** 程序 源代码 交互 ...
随机推荐
- Dynamics CRM 2015-Sign Out选项
If you are using CRM Online - there is a sign out button in the upper right corner (in the web versi ...
- Android中点击隐藏软键盘最佳方法——Android开发之路4
Android中点击隐藏软键盘最佳方法 实现功能:点击EditText,软键盘出现并且不会隐藏,点击或者触摸EditText以外的其他任何区域,软键盘被隐藏: 1.重写dispatchTouchEve ...
- webform(九)——JQuery基础(选择器、事件、DOM操作)
JQuery -- 一个js函数包 一.选择器 1.基本选择器 ①id选择器:# ②class选择器:. ③标签名选择:标签名 ④并列选择:用,隔开 ⑤后代选 ...
- 用lumen构建API的相关流程
概述 Lumen是一个基于Laravel的微框架,主要用于小型应用和微服务,专注于性能和速度的优化,该框架一个重要的应用就是构建 RESTAPI. 为什么用Lumen构建REST API Lumen访 ...
- JavaScript 事件
事件 概念:事件是可以被 JavaScript 侦测到的行为. JavaScript 使我们有能力创建动态页面.事件是可以被 JavaScript 侦测到的行为. 网页中的每个元素都可以产生某些可以触 ...
- SecondaryNameNode的工作流程
SecondaryNameNode是用来合并fsimage和edits文件来更新NameNode和metadata的. 其工作流程为: 1.secondary通知namenode切换edits文件 2 ...
- Java 加解密 AES DES TripleDes
package xxx.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypt ...
- LAMP坏境和LNMP环境安装Nagios4.1.1和基本配置
----------------------------------------以下内容为笔者生产环境的监控,安装都是经过一步步测试的-------------------------------- ...
- 安卓APP与智能硬件相结合的简易方案
第1章 概 述 (作者小波QQ463431476) (来源http://blog.chinaaet.com/zhaocundang/p/5100017645博客) (来源 http://www. ...
- js获取页面url中的各项值
一. 通过window.location获取各项参数 1.获取页面完整的url url = window.location.href; 2.获取页面的域名 host = window.location ...