aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service.UI/App.xaml.cs
blob: d223c60679de631e23d8a1a3d034e6b73e6b8f24 (plain)
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
using Notifications.Wpf;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using Tango.Logging;
using Tango.Settings;

namespace Tango.StubsUtils.Service.UI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F0STUBSUTI}");

        private WpfGlobalExceptionTrapper exceptionTrapper;
        private LogManager LogManager = LogManager.Default;
        private HashSet<String> _ignoredGlobalExceptions;

        public App() : base()
        {
            _ignoredGlobalExceptions = new HashSet<string>();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                //This is the first instance. Do nothing...
                mutex.ReleaseMutex();
            }
            else
            {
                MessageBox.Show("Tango Stubs Service is already running.");
                Environment.Exit(0);
            }

            //Set culture info.
            var enUSCulture = new CultureInfo("en-US");

            Thread.CurrentThread.CurrentCulture = enUSCulture;
            Thread.CurrentThread.CurrentUICulture = enUSCulture;
            CultureInfo.DefaultThreadCurrentCulture = enUSCulture;
            CultureInfo.DefaultThreadCurrentUICulture = enUSCulture;

            FrameworkElement.LanguageProperty.OverrideMetadata(
            typeof(FrameworkElement),
            new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

            base.OnStartup(e);

            exceptionTrapper = new WpfGlobalExceptionTrapper();
            exceptionTrapper.Initialize(this);
            exceptionTrapper.ApplicationCrashed += ExceptionTrapper_ApplicationCrashed;

            ServiceUISettings settings = SettingsManager.Default.GetOrCreate<ServiceUISettings>();
            settings.Save();
        }

        private void ExceptionTrapper_ApplicationCrashed(object sender, ApplicationCrashedEventArgs e)
        {
            e.TryRecover = true;

            List<String> ignoredExceptions = new List<string>()
            {
                "FocusVisualStyle",
                "ThreadAbortException",
                "A Task's exception(s) were not observed",
                "The calling thread must be STA, because many UI components require this."
            };

            String exceptionString = e.Exception.ToStringSafe();

            if (ignoredExceptions.Exists(x => exceptionString.Contains(x)))
            {
                return;
            }

            LogManager.Log(e.Exception, LogCategory.Critical, "Unexpected Application Error.");

            Application.Current.Dispatcher.Invoke(() =>
            {
                try
                {
                    LogManager.Log("Trying to notify the user about the crash...");
                    NotificationManager notification = new NotificationManager();
                    notification.Show(new NotificationContent()
                    {
                        Title = "Tango Stubs Service",
                        Message = $"Unexpected Application Error",
                        Type = NotificationType.Error
                    });
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error using the notification provider for notifying about application crash.");
                }
            });
        }
    }
}