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*************************************************** 程序 源代码 交互 ...
随机推荐
- 【代码笔记】iOS-页面调的时候隐藏工具条
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...
- linux的虚拟内存是4G,而每个进程都有自己独立的4G内存空间,怎么理解?
问: linux的虚拟内存是4G,而每个进程都有自己独立的4G内存空间,怎么理解? 每个进程所拥有的4G独立的虚拟内存空间是什么意思?linux系统的虚拟4G空间中,高位的1G是用于系统内核运行的,那 ...
- MP3文件ID3信息编辑器代码开源 - 开源研究系列文章
上次把磁性窗体的源码开源了,这次就开源另一个程序源码:MP3文件ID3信息编辑器.这个源码也比较简单,关键在于获取和写入MP3文件的这个ID3的信息即可. 这个操作信息编辑的就封装在MP3ID3.ba ...
- overflow:hidden与margin:0 auto之间的冲突
相对于父容器水平居中的代码margin:0 auto与overflow:hidden之间存在冲突.当这两个属性同时应用在一个DIV上时,在chrome浏览器中将无法居中.至于为啥我也不明白.
- idea缓存
昨天idea出现了一个奇怪的问题: 项目没有按我指定的配置运行,按cmd+:可以看输出.而是运行了配置包下的test环境的配置, 先一看,test环境被初始化为资源包并且在输出目录上, 先取消(fil ...
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- Everything(文件搜索神器)
前言 Everything官网: http://www.voidtools.com/ 软件版本: V1.3.4.686 (x64) 操作系统: windows 7/10 搜索FTP(内网)资源 比如内 ...
- Lambert(朗伯)光照模型 和Half Lambert的区别
Lambert-它不包括任何任何镜面属性,对粗糙物体来说,这项属性是非常有用的,它不会反射出周围的环境.Lambert材质可以是透明的,在光线追踪渲染中发生折射,但是如果没有镜面属性,该类型就不会发生 ...
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...