public void Run(string[] args, System.Reflection.Assembly executable)
		{		
			bool tracedExceptionThrown = false;				
			try
			{	
				using (SplashWindowThread splashThread = new SplashWindowThread(executable, false))
				{
					splashThread.ShowAsynchronously();			
					splashThread.Window.SetMarqueeMoving(true, true);
					
					ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Parsing command line...");

					// create a new command line parsing engine
					CommandLineParsingEngine pe = new CommandLineParsingEngine(args);
					
					// determine if we are going to keep the old versions
					bool keepOld = pe.ToBoolean("keepold");

					// the process id of an app
					int pid = pe.ToInt32("pid");

					// whether we should wait on the specified pid to die before launching new version
					bool wait = pe.ToBoolean("wait");

					ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Searching for runnable version...");

					// create a search for all of the subdirectories
					Search search = new Search("Versions", Application.StartupPath, "*", false, false);

					// find all of the directories
					DirectoryInfo[] directories = search.GetDirectories();

					// create versioned files around each directory that can be parsed to a version
					VersionedDirectory[] versionedDirectories = this.CreateVersionedFiles(directories);

					// if we have been instructed to wait on the process specified by pid to die, do it now
					if (wait && pid != 0)
					{
						try
						{
							// snag it and wait on it to exit
							Process p = Process.GetProcessById(pid);
							if (p != null)
							{
								ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Closing previous instance...");
								p.WaitForExit();
							}
						}
						catch(System.Exception systemException)
						{
							System.Diagnostics.Trace.WriteLine(systemException);
						}
					}
				
					ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Selecting latest runnable version...");

					// try and start the newest version
					VersionedDirectory versionStarted;
					bool startedVersion = this.StartNewestVersion(executable, versionedDirectories, out versionStarted, splashThread.Window);
					// this will fall back upon older versions until it runs out of versions or one of the versions starts
					if (!startedVersion)
					{
						string exeName = string.Format("{0}.exe", executable.GetName().Name);
						ExceptionEngine.DisplayException(null, "BootStrap failed for " + exeName, MessageBoxIcon.Stop,  MessageBoxButtons.OK, null, 
							"No suitable executable was found or able to be started.");
					}

					// if we're not keeping the old versions
					if (!keepOld)
					{
						// delete the older versions 
						if (!this.DeleteOlderVersions(versionedDirectories, versionStarted, splashThread.Window))
						{
							// um, who cares if we can't delete the older versions
							// also we need to see about security rights to the directories
						}
					}
					
					// if we started a version
					if (startedVersion)
						// notify that we are transferring control now to it...
						ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Transferring control to version " + versionStarted.Version.ToString() + "...");					
				}
			}
			catch(System.Exception systemException)
			{
				tracedExceptionThrown = true;
				System.Diagnostics.Trace.WriteLine(systemException);
				System.Windows.Forms.MessageBox.Show(null, systemException.ToString(), "Application Exiting");
				Application.ExitThread();
			}
			finally
			{
				System.Diagnostics.Trace.WriteLine("'" + Application.ProductName + (tracedExceptionThrown ? "' has terminated because of an exception." : "' has exited gracefully."));
			}
		}
Example #2
0
        public void Run(string[] args, System.Reflection.Assembly executable)
        {
            bool tracedExceptionThrown = false;

            try
            {
                using (SplashWindowThread splashThread = new SplashWindowThread(executable, false))
                {
                    splashThread.ShowAsynchronously();
                    splashThread.Window.SetMarqueeMoving(true, true);

                    ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Parsing command line...");

                    // create a new command line parsing engine
                    CommandLineParsingEngine pe = new CommandLineParsingEngine(args);

                    // determine if we are going to keep the old versions
                    bool keepOld = pe.ToBoolean("keepold");

                    // the process id of an app
                    int pid = pe.ToInt32("pid");

                    // whether we should wait on the specified pid to die before launching new version
                    bool wait = pe.ToBoolean("wait");

                    ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Searching for runnable version...");

                    // create a search for all of the subdirectories
                    Search search = new Search("Versions", Application.StartupPath, "*", false, false);

                    // find all of the directories
                    DirectoryInfo[] directories = search.GetDirectories();

                    // create versioned files around each directory that can be parsed to a version
                    VersionedDirectory[] versionedDirectories = this.CreateVersionedFiles(directories);

                    // if we have been instructed to wait on the process specified by pid to die, do it now
                    if (wait && pid != 0)
                    {
                        try
                        {
                            // snag it and wait on it to exit
                            Process p = Process.GetProcessById(pid);
                            if (p != null)
                            {
                                ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Closing previous instance...");
                                p.WaitForExit();
                            }
                        }
                        catch (System.Exception systemException)
                        {
                            System.Diagnostics.Trace.WriteLine(systemException);
                        }
                    }

                    ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Selecting latest runnable version...");

                    // try and start the newest version
                    VersionedDirectory versionStarted;
                    bool startedVersion = this.StartNewestVersion(executable, versionedDirectories, out versionStarted, splashThread.Window);
                    // this will fall back upon older versions until it runs out of versions or one of the versions starts
                    if (!startedVersion)
                    {
                        string exeName = string.Format("{0}.exe", executable.GetName().Name);
                        ExceptionEngine.DisplayException(null, "BootStrap failed for " + exeName, MessageBoxIcon.Stop, MessageBoxButtons.OK, null,
                                                         "No suitable executable was found or able to be started.");
                    }

                    // if we're not keeping the old versions
                    if (!keepOld)
                    {
                        // delete the older versions
                        if (!this.DeleteOlderVersions(versionedDirectories, versionStarted, splashThread.Window))
                        {
                            // um, who cares if we can't delete the older versions
                            // also we need to see about security rights to the directories
                        }
                    }

                    // if we started a version
                    if (startedVersion)
                    {
                        // notify that we are transferring control now to it...
                        ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Transferring control to version " + versionStarted.Version.ToString() + "...");
                    }
                }
            }
            catch (System.Exception systemException)
            {
                tracedExceptionThrown = true;
                System.Diagnostics.Trace.WriteLine(systemException);
                System.Windows.Forms.MessageBox.Show(null, systemException.ToString(), "Application Exiting");
                Application.ExitThread();
            }
            finally
            {
                System.Diagnostics.Trace.WriteLine("'" + Application.ProductName + (tracedExceptionThrown ? "' has terminated because of an exception." : "' has exited gracefully."));
            }
        }
		/// <summary>
		/// Runs the current instance of the hosting engine. This creates a blocking thread message loop until a SnapIn calls Application.ExitThread in the context of the hosting engine.
		/// </summary>
		/// <param name="args">The command line arguments to pass to the hosting engine. See the CommandLineParsingEngine for capabilities and syntaxes supported.</param>
		/// <param name="executable">The assembly for the executable that is starting the hosting engine.</param>
		public void Run(string[] args, System.Reflection.Assembly executable)
		{
			try
			{
				// if we are not the only instance		
				if (!_instanceManager.IsOnlyInstance)
				{
					// send the command line to the previous instance, and then bail
					_instanceManager.SendCommandLineToPreviousInstance(args);
					return;
				}

				_startingExecutable = executable;			
				_splashThread = new SplashWindowThread(executable);
				_splashThread.ShowAsynchronously();
				_splashThread.Window.SetMarqueeMoving(true, true);

				this.Run(args);
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
		}