예제 #1
0
        /// <summary>
        /// Called when an exit is requested.
        /// </summary>
        /// <param name="applicationFacade">
        /// The object that contains the methods that allow interaction
        /// with the kernel.
        /// </param>
        private static void OnExit(IAbstractApplications applicationFacade)
        {
            // If there is no application facade, then we're in
            // designer mode, or something else silly.
            if (applicationFacade == null)
            {
                return;
            }

            // the only catch is that the Shutdown method will return before
            // we know if the shutdown will be cancelled?
            applicationFacade.Shutdown();
        }
예제 #2
0
        private static int RunApplication(string[] arguments)
        {
            try
            {
                ShowHeader();
                LoadKernel();
                LogStartup();

                var options = CreateOptionSet();
                try
                {
                    options.Parse(arguments);
                }
                catch (OptionException e)
                {
                    s_Diagnostics.Log(
                        LevelToLog.Fatal,
                        ConsoleConstants.LogPrefix,
                        string.Format(
                            CultureInfo.InvariantCulture,
                            Resources.Log_Error_InvalidInputParameters_WithException,
                            e));

                    WriteErrorToConsole(Resources.Output_Error_InvalidInput);
                    return(InvalidCommandLineParametersExitCode);
                }

                if (string.IsNullOrWhiteSpace(s_ScriptFile) || s_ShouldDisplayHelp)
                {
                    ShowHelp(options);
                    return(HelpShownExitCode);
                }

                if (!File.Exists(s_ScriptFile))
                {
                    s_Diagnostics.Log(
                        LevelToLog.Fatal,
                        ConsoleConstants.LogPrefix,
                        string.Format(
                            CultureInfo.InvariantCulture,
                            Resources.Log_Error_ScripFileDoesNotExist,
                            s_ScriptFile));

                    WriteErrorToConsole(Resources.Output_Error_ScriptFileDoesNotExist);
                    return(InvalidScriptFileExitCode);
                }

                WriteInputParametersToLog(s_ScriptFile);
                System.Console.WriteLine(Resources.Output_Information_LoadingScriptFile);

                string text;
                using (var reader = new StreamReader(s_ScriptFile))
                {
                    text = reader.ReadToEnd();
                }

                System.Console.WriteLine(Resources.Output_Information_ExecutingScript);
                var executionPair = s_ScriptHost.Execute(SelectScriptLanguageFromFileExtension(s_ScriptFile), text, System.Console.Out);
                try
                {
                    CancellationTokenSource cancellationSource = executionPair.Item2;
                    System.Console.CancelKeyPress += (s, e) =>
                    {
                        var source = cancellationSource;
                        if (source != null)
                        {
                            source.Cancel();
                        }
                    };

                    executionPair.Item1.Wait();
                    return(NormalApplicationExitCode);
                }
                catch (AggregateException e)
                {
                    s_Diagnostics.Log(
                        LevelToLog.Fatal,
                        ConsoleConstants.LogPrefix,
                        string.Format(
                            CultureInfo.InvariantCulture,
                            Resources.Log_Error_ProcessingError_WithException,
                            e));

                    WriteErrorToConsole(Resources.Output_Error_WhileProcessing);
                    return(UnhandledExceptionApplicationExitCode);
                }
            }
            finally
            {
                if (s_ApplicationFacade != null)
                {
                    s_ApplicationFacade.Shutdown();
                }

                s_ShutdownEvent.WaitOne();
                if (s_UiContainer != null)
                {
                    s_UiContainer.Dispose();
                }
            }
        }