181 lines
7.8 KiB
C#
181 lines
7.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Threading;
|
|
|
|
namespace ZKuP
|
|
{
|
|
/// <summary>
|
|
/// Interaktionslogik für "App.xaml"
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
//#if !DEBUG
|
|
|
|
public App()
|
|
{
|
|
//AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
|
|
//#if !DEBUG
|
|
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; // Example 1
|
|
DispatcherUnhandledException += App_DispatcherUnhandledException;
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; // Example 3
|
|
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
|
|
|
|
|
|
//#endif
|
|
}
|
|
|
|
|
|
|
|
|
|
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
if (!e.Exception.ToString().Contains("System.Windows.Automation.ElementNotAvailableException:")
|
|
&& !e.Exception.ToString().Contains("signotec.STPadLibNet.STPadException: Zu dem ausgewählten Signierpad wurde keine Verbindung geöffnet."))
|
|
Log.WriteLog("1: " + e.Exception.ToString());
|
|
// Process unhandled exception
|
|
//if(MessageBox.Show(MainWindow, "Es ist ein unbehandelter Fehler aufgetreten\n\nMöchten Sie einen Bericht an den Entwickler der Anwendung senden?", "Unbehandelter Fehler", MessageBoxButton.YesNo, MessageBoxImage.Error, MessageBoxResult.Yes) == MessageBoxResult.Yes)
|
|
//{
|
|
// Helper.SendMail(e.Exception.ToString());
|
|
//}
|
|
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void Application_Startup(object sender, StartupEventArgs e)
|
|
{
|
|
|
|
|
|
//FirstChanceEx();
|
|
|
|
}
|
|
|
|
void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
|
|
{
|
|
if (SQL.isOnline && !e.Exception.ToString().Contains("System.Windows.Automation.ElementNotAvailableException:")
|
|
&& !e.Exception.ToString().Contains("System.OperationCanceledException")
|
|
&& !e.Exception.ToString().Contains("System.ObjectDisposedException")
|
|
&& !e.Exception.ToString().Contains("System.IO.IOException")
|
|
&& !e.Exception.ToString().Contains("signotec.STPadLibNet.STPadException: Zu dem ausgewählten Signierpad wurde keine Verbindung geöffnet.")
|
|
&& !e.Exception.ToString().Contains("Derzeit ist kein Layout"))
|
|
Log.WriteLog("2: " + e.Exception.ToString() + "\n" + e.ToString() + "\n" + (sender.GetType().ToString()));
|
|
|
|
//if (MessageBox.Show(MainWindow, "Es ist ein unbehandelter Fehler aufgetreten\n\nMöchten Sie einen Bericht an den Entwickler der Anwendung senden?", "Unbehandelter Fehler", MessageBoxButton.YesNo, MessageBoxImage.Error, MessageBoxResult.Yes) == MessageBoxResult.Yes)
|
|
//{
|
|
// Helper.SendMail(e.Exception.ToString());
|
|
//}
|
|
|
|
//ProcessError(e.Exception); - This could be used here to log ALL errors, even those caught by a Try/Catch block
|
|
}
|
|
|
|
void FirstChanceEx()
|
|
{
|
|
//AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
|
|
//{
|
|
// Log.WriteLog(eventArgs.Exception.ToString());
|
|
|
|
// if (MessageBox.Show(MainWindow, "Es ist ein unbehandelter Fehler aufgetreten\n\nMöchten Sie einen Bericht an den Entwickler der Anwendung senden?", "Unbehandelter Fehler", MessageBoxButton.YesNo, MessageBoxImage.Error, MessageBoxResult.Yes) == MessageBoxResult.Yes)
|
|
// {
|
|
// Helper.SendMail(eventArgs.Exception.ToString());
|
|
// }
|
|
//};
|
|
}
|
|
|
|
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
Exception ex = e.ExceptionObject as Exception;
|
|
|
|
if (!ex.ToString().Contains("System.Windows.Automation.ElementNotAvailableException:")
|
|
&& !ex.ToString().Contains("signotec.STPadLibNet.STPadException: Zu dem ausgewählten Signierpad wurde keine Verbindung geöffnet."))
|
|
Log.WriteLog("3: " + ex.ToString());
|
|
|
|
//if (MessageBox.Show(MainWindow, "Es ist ein unbehandelter Fehler aufgetreten\n\nMöchten Sie einen Bericht an den Entwickler der Anwendung senden?", "Unbehandelter Fehler", MessageBoxButton.YesNo, MessageBoxImage.Error, MessageBoxResult.Yes) == MessageBoxResult.Yes)
|
|
//{
|
|
// Helper.SendMail(ex.ToString());
|
|
//}
|
|
|
|
//MessageBox.Show(ex.Message, "Uncaught Thread Exception", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
|
}
|
|
|
|
//static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
|
|
//{
|
|
// Log.WriteLog(e.Exception.Message.ToString());
|
|
|
|
// // Log the exception, display it, etc
|
|
// if (MessageBox.Show("Es ist ein unbehandelter Fehler aufgetreten\n\nMöchten Sie einen Bericht an den Entwickler der Anwendung senden?", "Unbehandelter Fehler", MessageBoxButton.YesNo, MessageBoxImage.Error, MessageBoxResult.Yes) == MessageBoxResult.Yes)
|
|
// {
|
|
// Helper.SendMail(e.Exception.Message.ToString());
|
|
// }
|
|
|
|
//}
|
|
|
|
void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
|
|
{
|
|
if (!e.Exception.ToString().Contains("System.Windows.Automation.ElementNotAvailableException:")
|
|
&& !e.Exception.ToString().Contains("signotec.STPadLibNet.STPadException: Zu dem ausgewählten Signierpad wurde keine Verbindung geöffnet."))
|
|
Log.WriteLog("4: " + e.Exception.ToString());
|
|
//if (MessageBox.Show("Es ist ein unbehandelter Fehler aufgetreten\n\nMöchten Sie einen Bericht an den Entwickler der Anwendung senden?", "Unbehandelter Fehler", MessageBoxButton.YesNo, MessageBoxImage.Error, MessageBoxResult.Yes) == MessageBoxResult.Yes)
|
|
//{
|
|
// Helper.SendMail(e.Exception.Message.ToString());
|
|
//}
|
|
e.SetObserved();
|
|
}
|
|
|
|
|
|
//private static Mutex _mutex;
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
|
|
private const int SW_RESTORE = 9;
|
|
|
|
//protected override void OnStartup(StartupEventArgs e)
|
|
//{
|
|
// const string mutexName = "ZKuPMutex";
|
|
|
|
// bool createdNew;
|
|
// _mutex = new Mutex(true, mutexName, out createdNew);
|
|
|
|
// if (!createdNew)
|
|
// {
|
|
// // Another instance is already running
|
|
// BringOtherInstanceToFront();
|
|
// Environment.Exit(0); // Exit this instance
|
|
// return;
|
|
// }
|
|
|
|
// base.OnStartup(e);
|
|
//}
|
|
|
|
public static void BringOtherInstanceToFront()
|
|
{
|
|
var current = Process.GetCurrentProcess();
|
|
var other = Process.GetProcessesByName(current.ProcessName)
|
|
.FirstOrDefault(p => p.Id != current.Id);
|
|
|
|
if (other != null)
|
|
{
|
|
IntPtr hWnd = other.MainWindowHandle;
|
|
if (hWnd != IntPtr.Zero)
|
|
{
|
|
ShowWindow(hWnd, SW_RESTORE);
|
|
SetForegroundWindow(hWnd);
|
|
}
|
|
}
|
|
}
|
|
//#endif
|
|
}
|
|
}
|