/// <summary> /// Runs the application. /// </summary> public void Run() { try { // mbr - 21-07-2006 - added. if (this.CheckForOtherInstances) { // mbr - 22-06-2006 - this can fail? string name = null; try { // mbr - 20-04-2006 - is a version of this application running? Process thisProcess = Process.GetCurrentProcess(); if (thisProcess == null) { throw new InvalidOperationException("thisProcess is null."); } // find others with this name... name = thisProcess.ProcessName; Process[] otherProcesses = Process.GetProcessesByName(thisProcess.ProcessName); if (otherProcesses.Length > 1) { // raise an event... CancelEventArgs otherE = new CancelEventArgs(false); this.OnOtherProcessRunning(otherE); if (otherE.Cancel) { // TODO: activate the existing application... Alert.ShowInformation(null, "The application is already running."); // quit... return; } } } catch (Exception ex) { // mbr - 22-06-2006 - a blank name in the warning is indicative that the name of the process // could not be determined; if (this.Log.IsWarnEnabled) { this.Log.Warn(string.Format("Failed to check for processes with name '{0}'.", name), ex); } } } // get the last exception... Exception lastFailure = this.GetFailureException(); // reset... this.SetFailureException(null); // check... bool enableVisualStyles = true; if (lastFailure is System.Runtime.InteropServices.SEHException) { // ask! if (Alert.AskYesNoQuestion(null, "This application failed last time it was executed. The error may have been caused by enabling XP styles. Do you want to enable XP styles?") == DialogResult.Yes) { enableVisualStyles = true; } else { enableVisualStyles = false; } } // mbr - 06-09-2007 - added args... // Runtime.Start(this.CompanyName, this.ProductName, this.ProductModule, this.Version); Runtime.Start(this.CompanyName, this.ProductName, this.ProductModule, this.Version, this.StartArgs); // configure... CancelEventArgs e = new CancelEventArgs(); this.OnConfigureRuntime(e); // check... if (!(e.Cancel)) { // desktop... DesktopRuntime.Start(enableVisualStyles); DesktopRuntime.Current.DefaultFormIcon = this.DefaultFormIcon; // login... this.OnStartApplication(); } else { if (this.Log.IsWarnEnabled) { this.Log.Warn("Runtime configuration was cancelled."); } } } catch (Exception ex) { // log... if (Log.IsWarnEnabled) { Log.Warn(string.Format("A critical exception occurred:\r\n{0}", ex)); } // write the last exception... this.SetFailureException(ex); // show... Alert.ShowError(null, string.Format("A critical exception has stopped this application. The exception has been logged to: {0}", this.LastFailureFilePath), ex); // mbr - 14-04-2006 - added. Environment.ExitCode = this.FailureExitCode; } finally { try { if (Runtime.IsStarted) { Runtime.Current.Dispose(); } if (DesktopRuntime.IsStarted) { DesktopRuntime.Current.Dispose(); } } catch { // no-op... // TODO: log this somewhere... } } }