WPF App.xaml.cs常用模板,包括:异常捕获,App只能启动一次
程序员文章站
2022-06-11 16:13:43
App.xaml.cs中的代码每次都差不多,故特地将其整理出来直接复用: ......
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> 20 public partial class app : application 21 { 22 private loginwindow login = new loginwindow(); 23 private ilog logger; 24 25 static app() 26 { 27 log4net.config.xmlconfigurator.configure(); 28 } 29 30 public app() 31 { 32 logger = logmanager.getlogger(typeof(this)); 33 } 34 35 system.threading.mutex _mutex; 36 protected override void onstartup(startupeventargs e) 37 { 38 assembly assembly = assembly.getexecutingassembly(); 39 string mutexname = string.format(cultureinfo.invariantculture, "local\\{{{0}}}{{{1}}}", assembly.gettype().guid, assembly.getname().name); 40 bool ret = false; 41 _mutex = new system.threading.mutex(true, mutexname, out ret); 42 if (!ret) 43 { 44 this.logger.info("已经运行程序,激活至主窗口."); 45 handlerunninginstance(); 46 environment.exit(0); 47 return; 48 } 49 50 base.onstartup(e); 51 this.logger.info("app startup."); 52 this.shutdownmode = system.windows.shutdownmode.onexplicitshutdown; 53 54 appdomain.currentdomain.unhandledexception += currentdomain_unhandledexception; 55 this.dispatcherunhandledexception += app_dispatcherunhandledexception; 56 57 this.login.show(); 58 } 59 60 void currentdomain_unhandledexception(object sender, unhandledexceptioneventargs e) 61 { 62 try 63 { 64 var exception = e.exceptionobject as exception; 65 if (exception != null) 66 { 67 this.logger.fatalformat("非ui线程全局异常, message:{0}, error: {1}", exception.message, exception.tostring()); 68 } 69 } 70 catch (exception ex) 71 { 72 this.logger.fatalformat("非ui线程全局异常, message:{0}, error: {1}", ex.message, ex.tostring()); 73 } 74 } 75 76 void app_dispatcherunhandledexception(object sender, system.windows.threading.dispatcherunhandledexceptioneventargs e) 77 { 78 try 79 { 80 e.handled = true; 81 this.logger.fatalformat("ui线程全局异常:meassage:{0}, error: {1}", e.exception.message, e.exception.tostring()); 82 } 83 catch (exception ex) 84 { 85 this.logger.fatalformat("ui线程全局异常:meassage:{0}, error: {1}", ex.message, ex.tostring()); 86 } 87 } 88 89 90 protected override void onexit(exiteventargs e) 91 { 92 this.logger.info("app exit."); 93 94 base.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")] 104 private 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")] 113 private static extern bool setforegroundwindow(intptr hwnd); 114 115 static process runninginstance() 116 { 117 process current = process.getcurrentprocess(); 118 process[] processes = process.getprocessesbyname(current.processname); 119 foreach (process process in processes) 120 { 121 if (process.id != current.id) 122 { 123 if (process.mainmodule.filename == current.mainmodule.filename) 124 { 125 return process; 126 } 127 } 128 } 129 return null; 130 } 131 132 private const int sw_normal = 1; //正常弹出窗体 133 private const int sw_maximize = 3; //最大化弹出窗体 134 135 public static void handlerunninginstance() 136 { 137 var instance = runninginstance(); 138 if (instance != null) 139 { 140 showwindowasync(instance.mainwindowhandle, sw_normal); 141 setforegroundwindow(instance.mainwindowhandle); 142 } 143 } 144 } 145 }