-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
137 lines (116 loc) · 3.76 KB
/
Copy pathApp.xaml.cs
File metadata and controls
137 lines (116 loc) · 3.76 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections.Generic;
using System.IO;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
using MessageBoxButton = System.Windows.MessageBoxButton;
using MessageBoxImage = System.Windows.MessageBoxImage;
using StartupEventArgs = System.Windows.StartupEventArgs;
using DispatcherUnhandledExceptionEventArgs = System.Windows.Threading.DispatcherUnhandledExceptionEventArgs;
using ReadFolder.Services;
namespace ReadFolder;
public partial class App : Application
{
private static int _uiExceptionShown;
protected override void OnStartup(StartupEventArgs e)
{
DispatcherUnhandledException += OnDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
base.OnStartup(e);
var startupPath = ResolveStartupPath(e.Args);
LocalizationService.Instance.SetLanguage(null);
var window = new MainWindow(startupPath);
MainWindow = window;
window.Show();
}
private static string? ResolveStartupPath(IReadOnlyList<string> args)
{
if (args.Count == 0)
{
return null;
}
var rawPath = args[0];
if (string.IsNullOrWhiteSpace(rawPath))
{
return null;
}
if (Directory.Exists(rawPath))
{
return rawPath;
}
if (File.Exists(rawPath))
{
return Path.GetDirectoryName(rawPath);
}
return null;
}
private static void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
if (Interlocked.Exchange(ref _uiExceptionShown, 1) != 0)
{
e.Handled = true;
return;
}
MessageBox.Show(
BuildExceptionMessage("Unhandled UI exception", e.Exception),
"ReadFolder",
MessageBoxButton.OK,
MessageBoxImage.Error);
e.Handled = true;
}
private static void OnUnhandledException(object? sender, UnhandledExceptionEventArgs e)
{
if (Interlocked.Exchange(ref _uiExceptionShown, 1) != 0)
{
return;
}
if (e.ExceptionObject is Exception ex)
{
MessageBox.Show(
BuildExceptionMessage("Unhandled exception", ex),
"ReadFolder",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
private static void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
if (Interlocked.Exchange(ref _uiExceptionShown, 1) != 0)
{
e.SetObserved();
return;
}
MessageBox.Show(
BuildExceptionMessage("Background task exception", e.Exception),
"ReadFolder",
MessageBoxButton.OK,
MessageBoxImage.Warning);
e.SetObserved();
}
private static string BuildExceptionMessage(string title, Exception exception)
{
var lines = new List<string>
{
title + ":",
string.Empty,
exception.Message
};
var inner = exception.InnerException;
var depth = 1;
while (inner != null && depth <= 4)
{
lines.Add(string.Empty);
lines.Add($"InnerException {depth}:");
lines.Add(inner.Message);
inner = inner.InnerException;
depth++;
}
if (!string.IsNullOrWhiteSpace(exception.StackTrace))
{
lines.Add(string.Empty);
lines.Add("StackTrace:");
lines.Add(exception.StackTrace);
}
return string.Join(Environment.NewLine, lines);
}
}