Skip to content

Instantly share code, notes, and snippets.

@ialexpovad
Created April 18, 2023 07:16
Show Gist options
  • Save ialexpovad/469be88db6665be4455cab77b446b66f to your computer and use it in GitHub Desktop.
Save ialexpovad/469be88db6665be4455cab77b446b66f to your computer and use it in GitHub Desktop.
MessageBox closes immediately in DispatcherUnhandledException handler.
<Application x:Class="LOSD.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LOSD"
Startup="App_Startup"
Exit="App_Exit"
>
<Application.Resources>
</Application.Resources>
</Application>
public partial class App : Application
{
public static App m_instance;
private readonly string m_sObjectName; // readlyonly (const in C++)
bool isApplicationActive;
bool IsStartingUp;
public enum ApplicationExitCode
{
Success = 0,
Failure = 1,
CantWriteToApplicationLog = 2,
CantPersistApplicationState = 3
}
public App() : base()
{
m_instance = this;
m_sObjectName = string.Format("{0}_ProgramMutex", AssemblyInfo.AssemblyProduct);
ShutdownMode = ShutdownMode.OnExplicitShutdown;
IsStartingUp = true;
}
void App_Startup(object sender, StartupEventArgs e)
{
// Application is running
// Process command line args
bool startMinimized = false;
for (int i = 0; i != e.Args.Length; ++i)
{
if (e.Args[i] == "/StartMinimized")
{
startMinimized = true;
}
}
// Create main application window, starting minimized if specified
MainWindow mainWindow = new MainWindow();
if (startMinimized)
{
mainWindow.WindowState = WindowState.Minimized;
}
mainWindow.Show();
}
void App_Exit(object sender, ExitEventArgs e)
{
try
{
// Write entry to application log
if (e.ApplicationExitCode == (int)ApplicationExitCode.Success)
{
WriteApplicationLogEntry("Failure", e.ApplicationExitCode);
}
else
{
WriteApplicationLogEntry("Success", e.ApplicationExitCode);
}
}
catch
{
// Update exit code to reflect failure to write to application log
e.ApplicationExitCode = (int)ApplicationExitCode.CantWriteToApplicationLog;
}
// Persist application state
try
{
PersistApplicationState();
}
catch
{
// Update exit code to reflect failure to persist application state
e.ApplicationExitCode = (int)ApplicationExitCode.CantPersistApplicationState;
}
}
void WriteApplicationLogEntry(string message, int exitCode)
{
// Write log entry to file in isolated storage for the user
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
using (Stream stream = new IsolatedStorageFileStream("log.txt", FileMode.Append, FileAccess.Write, store))
using (StreamWriter writer = new StreamWriter(stream))
{
string entry = string.Format("{0}: {1} - {2}", message, exitCode, DateTime.Now);
writer.WriteLine(entry);
}
}
void PersistApplicationState()
{
// Persist application state to file in isolated storage for the user
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
using (Stream stream = new IsolatedStorageFileStream("state.txt", FileMode.Create, store))
using (StreamWriter writer = new StreamWriter(stream))
{
foreach (DictionaryEntry entry in this.Properties)
{
writer.WriteLine(entry.Value);
}
}
}
protected override void OnExit(ExitEventArgs e)
{
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/116bcd83-93bf-42f3-9bfe-da9e7de37546/messagebox-closes-immediately-in-dispatcherunhandledexception-handler?forum=wpf
// https://stackoverflow.com/questions/3935583/ask-the-user-before-closing-c-sharp-wpf-application
DispatcherUnhandledException -= new DispatcherUnhandledExceptionEventHandler(DispatcherUnhandledExceptionHandler);
try
{
base.OnExit(e);
} catch(Exception ex) { }
}
protected override void OnStartup(StartupEventArgs e)
{
DispatcherUnhandledException +=
new DispatcherUnhandledExceptionEventHandler(
DispatcherUnhandledExceptionHandler);
// WPF Bug Workaround: while we have no WPF window open we can`t show MessageBox.
Window WpfBugWindow = new Window()
{
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
WindowStyle = WindowStyle.None,
Top = 0,
Left = 0,
Width = 1,
Height = 1,
ShowInTaskbar = false
};
WpfBugWindow.Show();
try
{
base.OnStartup(e);
// If some problem occursduring the startup, than we have an exception here.
// InitializeMyApplication();
// And if there no any problem, than we continue...
IsStartingUp = false;
ShutdownMode = ShutdownMode.OnLastWindowClose;
WpfBugWindow.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can`t launch application because of:\r\n" + ex.ToString(),
"My Application", MessageBoxButton.OK, MessageBoxImage.Error);
WpfBugWindow.Close();
Shutdown(1);
}
}
void InitializeMyApplication()
{
// Let`s imagine that we have some exception during the application startup.
// For example:
throw new InvalidOperationException("Big problem occurs...");
}
void DispatcherUnhandledExceptionHandler(object sender, DispatcherUnhandledExceptionEventArgs e)
{
if (!e.Handled)
{
e.Handled = true;
if (!IsStartingUp) MessageBox.Show(e.Exception.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment