Index: branches/eraser6/Eraser/Program.cs
===================================================================
--- branches/eraser6/Eraser/Program.cs	(revision 1013)
+++ branches/eraser6/Eraser/Program.cs	(revision 1054)
@@ -299,5 +299,5 @@
 			//If no other instances are running, set up our pipe server so clients
 			//can connect and give us subsequent command lines.
-			if (IsAlreadyRunning)
+			if (IsFirstInstance)
 			{
 				try
@@ -308,5 +308,13 @@
 
 					//Initialise and run the program.
-					if (OnInitInstance(this) && MainForm != null)
+					bool ShowMainForm = OnInitInstance(this);
+					if (MainForm == null)
+						return false;
+
+					//Handle the exit instance event. This will occur when the main form
+					//has been closed.
+					mainForm.FormClosed += OnExitInstance;
+
+					if (ShowMainForm)
 						Application.Run(MainForm);
 					else
@@ -318,10 +326,9 @@
 						Application.Run();
 					}
+
 					return true;
 				}
 				finally
 				{
-					//Since the program was initialised we must let the program clean up.
-					OnExitInstance(this);
 					pipeServer.Abort();
 				}
@@ -455,5 +462,5 @@
 		/// Gets whether another instance of the program is already running.
 		/// </summary>
-		public bool IsAlreadyRunning
+		public bool IsFirstInstance
 		{
 			get
@@ -467,15 +474,5 @@
 		/// run is called if it is non-null and if its Visible property is true.
 		/// </summary>
-		public Form MainForm
-		{
-			get
-			{
-				return mainForm;
-			}
-			set
-			{
-				mainForm = value;
-			}
-		}
+		public Form MainForm { get; set; }
 
 		#region Events
@@ -547,4 +544,15 @@
 		/// <param name="sender">The sender of the event.</param>
 		private void OnExitInstance(object sender)
+		{
+			if (ExitInstance != null)
+				ExitInstance(sender);
+		}
+
+		/// <summary>
+		/// Broadcasts the ExitInstance event after getting the FormClosed event from
+		/// the application's main form.
+		/// </summary>
+		/// <param name="sender">The sender of the event.</param>
+		private void OnExitInstance(object sender, FormClosedEventArgs e)
 		{
 			if (ExitInstance != null)
