/// <summary>
        /// Open a window to let the user enter python code.
        /// </summary>
        /// <returns></returns>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var messageCopy = message;
            var gui         = new IronPythonConsole();

            gui.consoleControl.WithConsoleHost((host) =>
            {
                // now that the console is created and initialized, the script scope should
                // be accessible...
                new ScriptExecutor(RevitPythonShellApplication.GetConfig(), commandData, messageCopy, elements)
                .SetupEnvironment(host.Engine, host.Console.ScriptScope);

                host.Console.ScriptScope.SetVariable("__window__", gui);

                // run the initscript
                var initScript = RevitPythonShellApplication.GetInitScript();
                if (initScript != null)
                {
                    var scriptSource = host.Engine.CreateScriptSourceFromString(initScript, SourceCodeKind.Statements);
                    scriptSource.Execute(host.Console.ScriptScope);
                }
            });
            var commandCompletedEvent = new AutoResetEvent(false);
            var externalEventHandler  = new IronPythonExternalEventDispatcher(gui, commandCompletedEvent);
            var externalEvent         = ExternalEvent.Create(externalEventHandler);

            gui.consoleControl.WithConsoleHost((host) =>
            {
                var oldDispatcher = host.Console.GetCommandDispatcher();
                host.Console.SetCommandDispatcher((command) =>
                {
                    //externalEventHandler.Enqueue(() => oldDispatcher(command));
                    externalEventHandler.Enqueue(command);
                    externalEvent.Raise();
                    commandCompletedEvent.WaitOne();
                });

                host.Editor.SetCompletionDispatcher((command) =>
                {
                    externalEventHandler.Enqueue(command);
                    externalEvent.Raise();
                    commandCompletedEvent.WaitOne();
                });
            });
            gui.Topmost = true;
            gui.Title   = "RevitPythonShell (non-modal)";
            gui.Show();
            return(Result.Succeeded);
        }
        /// <summary>
        /// Open a window to let the user enter python code.
        /// </summary>
        /// <returns></returns>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var messageCopy = message;
            var gui = new IronPythonConsole();
            gui.consoleControl.WithConsoleHost((host) =>
            {
                // now that the console is created and initialized, the script scope should
                // be accessible...
                new ScriptExecutor(RevitPythonShellApplication.GetConfig(), commandData, messageCopy, elements)
                    .SetupEnvironment(host.Engine, host.Console.ScriptScope);

                host.Console.ScriptScope.SetVariable("__window__", gui);

                // run the initscript
                var initScript = RevitPythonShellApplication.GetInitScript();
                if (initScript != null)
                {
                    var scriptSource = host.Engine.CreateScriptSourceFromString(initScript, SourceCodeKind.Statements);
                    scriptSource.Execute(host.Console.ScriptScope);
                }
            });
            var commandCompletedEvent = new AutoResetEvent(false);
            var externalEventHandler = new IronPythonExternalEventDispatcher(gui, commandCompletedEvent);
            var externalEvent = ExternalEvent.Create(externalEventHandler);
            gui.consoleControl.WithConsoleHost((host) =>
            {
                var oldDispatcher = host.Console.GetCommandDispatcher();
                host.Console.SetCommandDispatcher((command) =>
                {
                    //externalEventHandler.Enqueue(() => oldDispatcher(command));
                    externalEventHandler.Enqueue(command);
                    externalEvent.Raise();
                    commandCompletedEvent.WaitOne();
                });

                host.Editor.SetCompletionDispatcher((command) =>
                {
                    externalEventHandler.Enqueue(command);
                    externalEvent.Raise();
                    commandCompletedEvent.WaitOne();
                });
            });
            gui.Topmost = true;
            gui.Title = "RevitPythonShell (non-modal)";
            gui.Show();
            return Result.Succeeded;
        }