-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
89 lines (78 loc) · 3 KB
/
Copy pathApp.xaml.cs
File metadata and controls
89 lines (78 loc) · 3 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
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace ArIED61850Tester;
public partial class App : Application
{
private static readonly object UiErrorSync = new();
private static string _lastUiErrorSignature = string.Empty;
private static DateTime _lastUiErrorUtc = DateTime.MinValue;
private static int _uiErrorHandlerActive;
private CancellationTokenSource? _updateCancellation;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
GridUxBehavior.Install();
FaultRecordUxBehavior.Install();
DispatcherUnhandledException += OnDispatcherUnhandledException;
TaskScheduler.UnobservedTaskException += (_, args) => args.SetObserved();
_updateCancellation = new CancellationTokenSource();
Dispatcher.BeginInvoke(
DispatcherPriority.ApplicationIdle,
new Action(() => _ = AppUpdateCoordinator.RunLazyAsync(_updateCancellation.Token)));
}
protected override void OnExit(ExitEventArgs e)
{
_updateCancellation?.Cancel();
_updateCancellation?.Dispose();
_updateCancellation = null;
base.OnExit(e);
}
private static void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// Mark the exception handled first. A modal MessageBox here can recursively trigger
// the same layout/binding exception and create an endless stack of dialogs.
e.Handled = true;
var exception = e.Exception;
var signature = $"{exception.GetType().FullName}|{exception.Message}";
var nowUtc = DateTime.UtcNow;
lock (UiErrorSync)
{
if (signature.Equals(_lastUiErrorSignature, StringComparison.Ordinal) &&
nowUtc - _lastUiErrorUtc < TimeSpan.FromSeconds(10))
{
Debug.WriteLine($"Suppressed repeated ARSAS UI error: {signature}");
return;
}
_lastUiErrorSignature = signature;
_lastUiErrorUtc = nowUtc;
}
if (Interlocked.Exchange(ref _uiErrorHandlerActive, 1) != 0)
return;
try
{
Debug.WriteLine(exception);
if (Current?.MainWindow is MainWindow mainWindow)
mainWindow.ReportUnexpectedUiError(exception);
}
catch (Exception reportingError)
{
Debug.WriteLine($"Failed to route ARSAS UI error to Diagnostics: {reportingError}");
}
finally
{
var dispatcher = Current?.Dispatcher;
if (dispatcher == null || dispatcher.HasShutdownStarted)
{
Interlocked.Exchange(ref _uiErrorHandlerActive, 0);
}
else
{
dispatcher.BeginInvoke(
DispatcherPriority.ContextIdle,
new Action(() => Interlocked.Exchange(ref _uiErrorHandlerActive, 0)));
}
}
}
}