Example #1
0
        /// <summary>
        /// Runs the program.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        /// <returns>
        /// Th exit code to return.
        /// </returns>
        public int Run(string[] args)
        {
            if (this.isDisposed)
            {
                throw new StartUpException("Program has already been disposed");
            }

            // Initialise all variables
            var location = Assembly.GetExecutingAssembly().Location;
            var fullName = typeof(AppRunner).FullName;
            if (fullName == null)
            {
                throw new StartUpException("Unable to retrieve full name of application runner");
            }

            // Start monitoring any file changes
            watcher.EnableRaisingEvents = true;

            // Begin the main application loop
            var result = 0;
            while (this.isRestarting)
            {
                this.isRestarting = false;

                // Load the domain and start the runner
                AppDomain runnerDomain;
                try
                {
                    runnerDomain = CreateNewDomain(useShadowCopying);
                }
                catch (FileLoadException)
                {
                    // Unable to use shadow-copying (no user profile?), therefore turn off shadow-copying
                    useShadowCopying = false;
                    runnerDomain = CreateNewDomain(false);
                }

                // Generate the new application runner
                this.appRunner = runnerDomain.CreateInstanceFromAndUnwrap(location, fullName) as AppRunner;
                if (appRunner == null)
                {
                    throw new StartUpException("Unable to initialise application runner");
                }

                // Start the runner and wait
                result = this.appRunner.Run(args, useShadowCopying);
                AppDomain.Unload(runnerDomain);

                // Allow any change events to finish (i.e. if multiple files are being copied)
                while (DateTime.Now < restartTime)
                {
                    Thread.Sleep(500);
                }
            }

            // Clean and return the result
            watcher.EnableRaisingEvents = false;
            return result;
        }
Example #2
0
        /// <summary>
        /// Runs the program.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        /// <returns>
        /// Th exit code to return.
        /// </returns>
        public int Run(string[] args)
        {
            if (this.isDisposed)
            {
                throw new StartUpException("Program has already been disposed");
            }

            // Initialise all variables
            var location = Assembly.GetExecutingAssembly().Location;
            var fullName = typeof(AppRunner).FullName;

            if (fullName == null)
            {
                throw new StartUpException("Unable to retrieve full name of application runner");
            }

            // Start monitoring any file changes
            watcher.EnableRaisingEvents = true;

            // Begin the main application loop
            var result = 0;

            while (this.isRestarting)
            {
                this.isRestarting = false;

                // Load the domain and start the runner
                AppDomain runnerDomain;
                try
                {
                    runnerDomain = CreateNewDomain(useShadowCopying);
                }
                catch (FileLoadException)
                {
                    // Unable to use shadow-copying (no user profile?), therefore turn off shadow-copying
                    useShadowCopying = false;
                    runnerDomain     = CreateNewDomain(false);
                }

                // Generate the new application runner
                this.appRunner = runnerDomain.CreateInstanceFromAndUnwrap(location, fullName) as AppRunner;
                if (appRunner == null)
                {
                    throw new StartUpException("Unable to initialise application runner");
                }

                // Start the runner and wait
                result = this.appRunner.Run(args, useShadowCopying);
                AppDomain.Unload(runnerDomain);

                // Allow any change events to finish (i.e. if multiple files are being copied)
                while (DateTime.Now < restartTime)
                {
                    Thread.Sleep(500);
                }
            }

            // Clean and return the result
            watcher.EnableRaisingEvents = false;
            return(result);
        }