2017-01-08 07:54:09 +00:00
using System ;
2016-08-22 01:05:41 +00:00
using System.Threading ;
2014-06-28 21:22:05 +00:00
using System.Windows.Forms ;
2017-01-08 07:54:09 +00:00
namespace PKHeX.WinForms
2014-06-28 21:22:05 +00:00
{
2016-07-09 01:14:15 +00:00
internal static class Program
2014-06-28 21:22:05 +00:00
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
2016-07-09 01:14:15 +00:00
private static void Main ( )
2014-06-28 21:22:05 +00:00
{
2016-08-22 01:05:41 +00:00
// Add the event handler for handling UI thread exceptions to the event.
2017-01-08 07:54:09 +00:00
Application . ThreadException + = UIThreadException ;
2016-08-22 01:05:41 +00:00
// Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
Application . SetUnhandledExceptionMode ( UnhandledExceptionMode . CatchException ) ;
// Add the event handler for handling non-UI thread exceptions to the event.
2017-01-08 07:54:09 +00:00
AppDomain . CurrentDomain . UnhandledException + = CurrentDomain_UnhandledException ;
2016-08-22 01:05:41 +00:00
// Run the application
2014-06-28 21:22:05 +00:00
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
2015-09-21 03:34:09 +00:00
Application . Run ( new Main ( ) ) ;
2014-06-28 21:22:05 +00:00
}
2016-08-22 01:05:41 +00:00
// Handle the UI exceptions by showing a dialog box, and asking the user whether or not they wish to abort execution.
private static void UIThreadException ( object sender , ThreadExceptionEventArgs t )
{
DialogResult result = DialogResult . Cancel ;
try
{
2016-08-22 01:23:32 +00:00
// Todo: make this translatable
2017-01-08 23:27:00 +00:00
ErrorWindow . ShowErrorDialog ( "An unhandled exception has occurred.\nYou can continue running PKHeX, but please report this error." , t . Exception , true ) ;
2016-08-22 01:05:41 +00:00
}
catch
{
try
{
// Todo: make this translatable
2017-01-08 23:27:00 +00:00
MessageBox . Show ( "A fatal error has occurred in PKHeX, and the details could not be displayed. Please report this to the author." , "PKHeX Error" , MessageBoxButtons . OK , MessageBoxIcon . Stop ) ;
2016-08-22 01:05:41 +00:00
}
finally
{
Application . Exit ( ) ;
}
}
// Exits the program when the user clicks Abort.
if ( result = = DialogResult . Abort )
Application . Exit ( ) ;
}
// Handle the UI exceptions by showing a dialog box, and asking the user whether
// or not they wish to abort execution.
// NOTE: This exception cannot be kept from terminating the application - it can only
// log the event, and inform the user about it.
private static void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs e )
{
try
{
var ex = ( Exception ) e . ExceptionObject ;
2016-08-22 01:23:32 +00:00
// Todo: make this translatable
2017-01-08 23:27:00 +00:00
ErrorWindow . ShowErrorDialog ( "An unhandled exception has occurred.\nPKHeX must now close." , ex , false ) ;
2016-08-22 01:05:41 +00:00
}
catch
{
try
{
// Todo: make this translatable
2017-01-08 23:27:00 +00:00
MessageBox . Show ( "A fatal non-UI error has occurred in PKHeX, and the details could not be displayed. Please report this to the author." , "PKHeX Error" , MessageBoxButtons . OK , MessageBoxIcon . Stop ) ;
2016-08-22 01:05:41 +00:00
}
finally
{
Application . Exit ( ) ;
}
}
}
2014-06-28 21:22:05 +00:00
}
}