-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathExceptionHandler.cs
More file actions
73 lines (61 loc) · 2.41 KB
/
ExceptionHandler.cs
File metadata and controls
73 lines (61 loc) · 2.41 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
// -------------------------------------------------------------------------------------------
// <copyright file="ExceptionHandler.cs" company="MapWindow OSS Team - www.mapwindow.org">
// MapWindow OSS Team - 2016
// </copyright>
// -------------------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Threading;
using MW5.Forms;
using MW5.Shared;
namespace MW5
{
public static class ExceptionHandler
{
public static void Attach()
{
if (Debugger.IsAttached) return;
// main UI thread only
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_ThreadException;
// in delegates called by Invoke or BeginInvoke.
Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
// last resort, the app will be terminated anyway
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
Logger.Current.Error("Application_ThreadException", e.Exception);
using (var form = new ErrorView(e.Exception, false))
{
if (form.ShowDialog() == DialogResult.OK)
{
Application.Exit();
}
}
}
private static void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Logger.Current.Error("CurrentDispatcher_UnhandledException", e.Exception);
using (var form = new ErrorView(e.Exception, false))
{
if (form.ShowDialog() == DialogResult.OK)
{
Application.Exit();
}
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var ex = e.ExceptionObject as Exception;
Logger.Current.Error("CurrentDomain_UnhandledException", ex);
using (var form = new ErrorView(ex, true))
{
form.ShowDialog();
Application.Exit();
}
}
}
}