2009-05-22 14:37:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2009-07-26 23:22:00 +00:00
|
|
|
|
using System.Security.Permissions;
|
2009-05-22 14:37:50 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Jellyfish.Library
|
|
|
|
|
{
|
|
|
|
|
public class ApplicationBase : Application
|
|
|
|
|
{
|
2009-07-26 23:22:00 +00:00
|
|
|
|
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
|
2009-05-22 14:37:50 +00:00
|
|
|
|
public ApplicationBase(string name)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
|
2009-12-13 05:53:53 +00:00
|
|
|
|
DispatcherUnhandledException += OnApplicationDispatcherUnhandledException;
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += OnAppDomainUnhandledException;
|
2009-05-22 14:37:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-13 05:53:53 +00:00
|
|
|
|
private void OnApplicationDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
2009-05-22 14:37:50 +00:00
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(GetExceptionMessage(e.Exception), GetExceptionCaption("Application Dispatcher Exception", true));
|
|
|
|
|
Shutdown();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-13 05:53:53 +00:00
|
|
|
|
private void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
|
2009-05-22 14:37:50 +00:00
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(GetExceptionMessage(e.ExceptionObject as Exception), GetExceptionCaption("AppDomain Exception", e.IsTerminating));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetExceptionCaption(string title, bool isTerminating)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder 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)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder message = new StringBuilder();
|
|
|
|
|
if (exception != null)
|
|
|
|
|
{
|
|
|
|
|
message.Append(exception.Message.ToString());
|
|
|
|
|
message.Append(Environment.NewLine);
|
|
|
|
|
message.Append(Environment.NewLine);
|
|
|
|
|
message.Append(exception.ToString()); // includes stack trace
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return message.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name { get; private set; }
|
|
|
|
|
}
|
|
|
|
|
}
|