2010-04-04 00:12:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2010-05-19 23:42:10 +00:00
|
|
|
|
using System.Security;
|
2010-04-04 00:12:01 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Jellyfish.Library
|
|
|
|
|
{
|
|
|
|
|
public class ApplicationBase : Application
|
|
|
|
|
{
|
2010-05-19 23:42:10 +00:00
|
|
|
|
[SecurityCritical]
|
2010-05-28 10:48:08 +00:00
|
|
|
|
public ApplicationBase(string name = null)
|
2010-04-04 00:12:01 +00:00
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
|
|
|
|
|
DispatcherUnhandledException += OnApplicationDispatcherUnhandledException;
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += OnAppDomainUnhandledException;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnApplicationDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
|
|
|
|
{
|
2010-05-28 10:48:08 +00:00
|
|
|
|
MessageBox.Show(GetExceptionMessage(e.Exception), GetExceptionCaption("Application Dispatcher Exception", isTerminating: true));
|
2010-04-04 00:12:01 +00:00
|
|
|
|
e.Handled = true;
|
|
|
|
|
Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(GetExceptionMessage(e.ExceptionObject as Exception), GetExceptionCaption("AppDomain Exception", e.IsTerminating));
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-28 10:48:08 +00:00
|
|
|
|
private string GetExceptionCaption(string title, bool isTerminating = false)
|
2010-04-04 00:12:01 +00:00
|
|
|
|
{
|
|
|
|
|
var caption = new StringBuilder();
|
|
|
|
|
caption.AppendFormat("[{0}] ", Process.GetCurrentProcess().Id);
|
|
|
|
|
if (!string.IsNullOrEmpty(Name))
|
|
|
|
|
{
|
|
|
|
|
caption.Append(Name);
|
|
|
|
|
caption.Append(" ");
|
|
|
|
|
}
|
|
|
|
|
caption.Append(title);
|
|
|
|
|
if (isTerminating)
|
|
|
|
|
{
|
|
|
|
|
caption.Append(" (Terminating)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return caption.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetExceptionMessage(Exception exception)
|
|
|
|
|
{
|
|
|
|
|
var message = new StringBuilder();
|
|
|
|
|
if (exception != null)
|
|
|
|
|
{
|
|
|
|
|
message.Append(exception.Message.ToString());
|
|
|
|
|
message.Append(Environment.NewLine);
|
|
|
|
|
message.Append(exception.StackTrace.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return message.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name { get; private set; }
|
|
|
|
|
}
|
|
|
|
|
}
|