WPF App.xaml.cs常用模板,包括(异常捕获,App只能启动一次)

【WPF App.xaml.cs常用模板,包括(异常捕获,App只能启动一次)】少年击剑更吹箫,剑气箫心一例消。这篇文章主要讲述WPF App.xaml.cs常用模板,包括:异常捕获,App只能启动一次相关的知识,希望能为你提供帮助。
原文:WPF App.xaml.cs常用模板,包括:异常捕获,App只能启动一次App.xaml.cs中的代码每次都差不多,故特地将其整理出来直接复用:

5 using System; 6 using System.Configuration; 7 using System.Diagnostics; 8 using System.Globalization; 9 using System.Net; 10 using System.Net.Sockets; 11 using System.Reflection; 12 using System.Runtime.InteropServices; 13 using System.Windows; 14 15 namespace WpfDemo 16 { 17/// < summary> 18/// App.xaml 的交互逻辑 19/// < /summary> 20public partial class App : Application 21{ 22private LoginWindow login = new LoginWindow(); 23private ILog logger; 24 25static App() 26{ 27log4net.Config.XmlConfigurator.Configure(); 28} 29 30public App() 31{ 32logger = LogManager.GetLogger(typeof(this)); 33} 34 35System.Threading.Mutex _mutex; 36protected override void OnStartup(StartupEventArgs e) 37{ 38Assembly assembly = Assembly.GetExecutingAssembly(); 39string mutexName = string.Format(CultureInfo.InvariantCulture, "Local\\{{{0}}}{{{1}}}", assembly.GetType().GUID, assembly.GetName().Name); 40bool ret = false; 41_mutex = new System.Threading.Mutex(true, mutexName, out ret); 42if (!ret) 43{ 44this.logger.Info("已经运行程序,激活至主窗口."); 45HandleRunningInstance(); 46Environment.Exit(0); 47return; 48} 49 50base.OnStartup(e); 51this.logger.Info("App startup."); 52this.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown; 53 54AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; 55this.DispatcherUnhandledException += App_DispatcherUnhandledException; 56 57this.login.Show(); 58} 59 60void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 61{ 62try 63{ 64var exception = e.ExceptionObject as Exception; 65if (exception != null) 66{ 67this.logger.FatalFormat("非UI线程全局异常, Message:{0}, Error: {1}", exception.Message, exception.ToString()); 68} 69} 70catch (Exception ex) 71{ 72this.logger.FatalFormat("非UI线程全局异常, Message:{0}, Error: {1}", ex.Message, ex.ToString()); 73} 74} 75 76void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) 77{ 78try 79{ 80e.Handled = true; 81this.logger.FatalFormat("UI线程全局异常:Meassage:{0}, Error: {1}", e.Exception.Message, e.Exception.ToString()); 82} 83catch (Exception ex) 84{ 85this.logger.FatalFormat("UI线程全局异常:Meassage:{0}, Error: {1}", ex.Message, ex.ToString()); 86} 87} 88 89 90protected override void OnExit(ExitEventArgs e) 91{ 92this.logger.Info("App exit."); 93 94base.OnExit(e); 95} 96 97///< summary> 98/// 该函数设置由不同线程产生的窗口的显示状态 99/// < /summary> 100/// < param name="hWnd"> 窗口句柄< /param> 101/// < param name="cmdShow"> 指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分< /param> 102/// < returns> 如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零< /returns> 103[DllImport("User32.dll")] 104private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); 105 106/// < summary> 107///该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。 108///系统给创建前台窗口的线程分配的权限稍高于其他线程。 109/// < /summary> 110/// < param name="hWnd"> 将被激活并被调入前台的窗口句柄< /param> 111/// < returns> 如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零< /returns> 112[DllImport("User32.dll")] 113private static extern bool SetForegroundWindow(IntPtr hWnd); 114 115static Process RunningInstance() 116{ 117Process current = Process.GetCurrentProcess(); 118Process[] processes = Process.GetProcessesByName(current.ProcessName); 119foreach (Process process in processes) 120{ 121if (process.Id != current.Id) 122{ 123if (process.MainModule.FileName == current.MainModule.FileName) 124{ 125return process; 126} 127} 128} 129return null; 130} 131 132private const int SW_NORMAL = 1; //正常弹出窗体 133private const int SW_MAXIMIZE = 3; //最大化弹出窗体 134 135public static void HandleRunningInstance() 136{ 137var instance = RunningInstance(); 138if (instance != null) 139{ 140ShowWindowAsync(instance.MainWindowHandle, SW_NORMAL); 141SetForegroundWindow(instance.MainWindowHandle); 142} 143} 144} 145 }

 


    推荐阅读