/// <inheritdoc /> /// <summary> /// Hook for processing the start of the application's lifecycle to set up required state /// </summary> /// <param name="args">Arguments providing additional information about the starting of the application</param> protected override void OnStartup(StartupEventArgs args) { base.OnStartup(args); // Initialize the logger for the application if (!LoggingUtilities.InitializeLogger()) { DoShutdown(ResultCode.LoggerFailure); return; } // Hook up an exception handler for processing unhandled exceptions thrown on the dispatcher thread DispatcherUnhandledException += DispatcherExceptionHandler; try { // Log the user in. Because the login prompt is the first window displayed by the application, it's incorrectly // interpreted to be the main window, meaning that the default behavior of the application will be to exit when // the window closes. Since this isn't the desired behavior here, the shutdown mode is temporarily changed for // the duration of time that the login window is open. Current.ShutdownMode = ShutdownMode.OnExplicitShutdown; var loginViewModel = new LoginViewModel(); new LoginWindow { DataContext = loginViewModel }.ShowDialog(); if (!loginViewModel.DidUserLogin) { if (Logger.IsInfoEnabled) { Logger.Info("User aborted login; exiting application"); } DoShutdown(); return; } Current.ShutdownMode = ShutdownMode.OnMainWindowClose; if (Logger.IsInfoEnabled) { Logger.InfoFormat("Logging in user with username {0}", loginViewModel.Username); } // Create and initialize the main viewmodel of the application, establishing a connection to the SMTP server _viewModel = new MainViewModel(); if (!_viewModel.Initialize(new MailAddress(loginViewModel.Username), loginViewModel.Password)) { if (Logger.IsWarnEnabled) { Logger.Warn("Initialization of the backing viewmodel failed"); } DoShutdown(ResultCode.LifecycleFailure); return; } var mainWindow = new MainWindow { DataContext = _viewModel, }; mainWindow.Show(); } catch (Exception ex) { if (Logger.IsErrorEnabled) { Logger.Error("An exception occurred during startup", ex); } DoShutdown(ResultCode.LifecycleFailure); } }