forked from cdwcgt/GTA5onlineTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
82 lines (70 loc) · 2.73 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using GTA5OnlineTools.Common.Utils;
namespace GTA5OnlineTools;
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public static Mutex AppMainMutex;
protected override void OnStartup(StartupEventArgs e)
{
AppMainMutex = new Mutex(true, ResourceAssembly.GetName().Name, out var createdNew);
if (createdNew)
{
RegisterEvents();
base.OnStartup(e);
}
else
{
MessageBox.Show("请不要重复打开,程序已经运行\n如果一直提示,请到\"任务管理器-详细信息(win7为进程)\"里结束本程序",
"警告", MessageBoxButton.OK, MessageBoxImage.Warning);
Current.Shutdown();
}
}
private void RegisterEvents()
{
// UI线程未捕获异常处理事件(UI主线程)
DispatcherUnhandledException += App_DispatcherUnhandledException;
// 非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
FileUtil.SaveErrorLog(str);
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
FileUtil.SaveErrorLog(str);
}
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
FileUtil.SaveErrorLog(str);
}
/// <summary>
/// 生成自定义异常消息
/// </summary>
/// <param name="ex">异常对象</param>
/// <param name="backStr">备用异常消息:当ex为null时有效</param>
/// <returns>异常字符串文本</returns>
private static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
return sb.ToString();
}
}