aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs
blob: a7596e16c2445e6406a29243eb25084d6f7d7681 (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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pse
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Tango.Core.Helpers;
using Tango.BL.Entities;
using Tango.Logging;
using Tango.MachineStudio.UI.Windows;
using Tango.Settings;

namespace Tango.MachineStudio.UI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private WpfGlobalExceptionTrapper exceptionTrapper;
        private LogManager LogManager = LogManager.Default;

        protected override void OnStartup(StartupEventArgs e)
        {
            LogManager.Log("Application Started...");

            base.OnStartup(e);

            LogManager.Categories.Clear();

            if (SettingsManager.Default.MachineStudio.LoggingCategories.Count == 0)
            {
                SettingsManager.Default.MachineStudio.LoggingCategories.Add(LogCategory.Critical);
                SettingsManager.Default.MachineStudio.LoggingCategories.Add(LogCategory.Error);
                SettingsManager.Default.MachineStudio.LoggingCategories.Add(LogCategory.Info);
                SettingsManager.Default.MachineStudio.LoggingCategories.Add(LogCategory.Warning);
            }

            LogManager.Categories.AddRange(SettingsManager.Default.MachineStudio.LoggingCategories);

            LogManager.RegisterLogger(new VSOutputLogger());
            LogManager.RegisterLogger(new FileLogger());

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

        #region Global Exception Trapping

        /// <summary>
        /// Handles the ApplicationCrashed event of the ExceptionTrapper.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ApplicationCrashedEventArgs"/> instance containing the event data.</param>
        private void ExceptionTrapper_ApplicationCrashed(object sender, ApplicationCrashedEventArgs e)
        {
            try
            {
                if (Application.Current == null)
                {
                    new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
                }
            }
            catch { }

            Application.Current.Dispatcher.Invoke(() =>
            {
                ExceptionWindow exWin = new ExceptionWindow(e.Exception);
                exWin.ShowDialog();

                switch (exWin.Resolution)
                {
                    case ExceptionResolutions.Ignore:
                        e.TryRecover = true;
                        break;
                    case ExceptionResolutions.Restart:
                        e.TryRecover = false;
                        LogManager.Log("User selection was to restart the application. Restarting...");
                        Process.Start(Application.ResourceAssembly.Location);
                        Environment.Exit(0);
                        break;
                    case ExceptionResolutions.Shutdown:
                        e.TryRecover = false;
                        LogManager.Log("User selection was to shutdown the application. Restarting...");
                        Environment.Exit(0);
                        break;
                }
            });
        }

        #endregion
    }
}