Exemplo n.º 1
0
        private void BackgroundExecute(BackgroundExecuteDelegate action, bool csharpCode)
        {
            string documentText = Document.Text;

            IsEnabled = false;
            if (Executing != null)
            {
                Executing(true);
            }
            Task.Run(() =>
            {
                initializedEvent.WaitOne();

                try
                {
                    string textOutput, errorOutput;
                    IEnumerable <object> objectOutput;

                    action(documentText, out textOutput, out errorOutput, out objectOutput);
                    Dispatcher.InvokeAsync(() =>
                    {
                        if (!string.IsNullOrEmpty(errorOutput))
                        {
                            if (CommandFailed != null)
                            {
                                CommandFailed(csharpCode, textOutput, errorOutput);
                            }
                        }
                        else
                        {
                            if (CommandExecuted != null)
                            {
                                CommandExecuted(csharpCode, textOutput, objectOutput);
                            }
                            Document.Text = "";
                        }

                        IsEnabled = true;
                        if (Executing != null)
                        {
                            Executing(false);
                        }
                    });
                }
                catch (ExitRequestedException)
                {
                    Dispatcher.InvokeAsync(() =>
                    {
                        if (CloseRequested != null)
                        {
                            CloseRequested();
                        }
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            });
        }
Exemplo n.º 2
0
        protected override void OnExecuteCSharpScript()
        {
            BackgroundExecute((string _documentText, out string _textOutput, out string _errorOutput, out IEnumerable <object> _result) =>
            {
                BackgroundExecuteDelegate scriptExecution = (string documentText, out string textOutput, out string errorOutput, out IEnumerable <object> result) =>
                {
                    // Setting results
                    textOutput  = "";
                    errorOutput = "";

                    // Execution code
                    var oldOut      = Console.Out;
                    var oldError    = Console.Error;
                    var oldGraphics = Context.Graphics;

                    try
                    {
                        Context.Graphics = new Graphics(Dispatcher);
                        using (StringWriter writer = new StringWriter())
                        {
                            Console.SetOut(writer);
                            Console.SetError(writer);

                            DebugOutput captureFlags = DebugOutput.Normal | DebugOutput.Error | DebugOutput.Warning | DebugOutput.Verbose
                                                       | DebugOutput.Prompt | DebugOutput.PromptRegisters | DebugOutput.ExtensionWarning | DebugOutput.Debuggee
                                                       | DebugOutput.DebuggeePrompt | DebugOutput.Symbols | DebugOutput.Status;
                            var callbacks = DebuggerOutputToTextWriter.Create(Console.Out, captureFlags);

                            InteractiveExecution.scriptBase._UiActionExecutor_ = (action) => Dispatcher.Invoke(action);
                            using (OutputCallbacksSwitcher switcher = OutputCallbacksSwitcher.Create(callbacks))
                            {
                                InteractiveExecution.UnsafeInterpret(documentText);
                                writer.Flush();
                                textOutput = writer.GetStringBuilder().ToString();
                            }
                        }

                        UpdateScriptCode();
                    }
                    catch (Microsoft.CodeAnalysis.Scripting.CompilationErrorException ex)
                    {
                        StringBuilder sb = new StringBuilder();

                        sb.AppendLine("Compile errors:");
                        foreach (var error in ex.Diagnostics)
                        {
                            sb.AppendLine(error.ToString());
                        }

                        errorOutput = sb.ToString();
                    }
                    catch (ExitRequestedException)
                    {
                        throw;
                    }
                    catch (AggregateException ex)
                    {
                        if (ex.InnerException is ExitRequestedException)
                        {
                            throw ex.InnerException;
                        }
                        errorOutput = ex.ToString();
                    }
                    catch (TargetInvocationException ex)
                    {
                        if (ex.InnerException is ExitRequestedException)
                        {
                            throw ex.InnerException;
                        }
                        errorOutput = ex.InnerException.ToString();
                    }
                    catch (Exception ex)
                    {
                        errorOutput = ex.ToString();
                    }
                    finally
                    {
                        Console.SetError(oldError);
                        Console.SetOut(oldOut);
                        Context.Graphics = oldGraphics;
                        result           = results;
                        results          = new List <object>();
                        InteractiveExecution.scriptBase._UiActionExecutor_ = null;
                    }
                };

                // Check if we should execute script code in STA thread
                if (InteractiveExecution.scriptBase.ForceStaExecution)
                {
                    string tempTextOutput           = null;
                    string tempErrorOutput          = null;
                    IEnumerable <object> tempResult = null;

                    InteractiveWindow.ExecuteInSTA(() =>
                    {
                        scriptExecution(_documentText, out tempTextOutput, out tempErrorOutput, out tempResult);
                    });
                    _textOutput  = tempTextOutput;
                    _errorOutput = tempErrorOutput;
                    _result      = tempResult;
                }
                else
                {
                    scriptExecution(_documentText, out _textOutput, out _errorOutput, out _result);
                }
            }, true);
        }
        private void BackgroundExecute(BackgroundExecuteDelegate action, bool csharpCode)
        {
            string documentText = Document.Text;

            IsEnabled = false;
            if (Executing != null)
                Executing(true);
            Task.Run(() =>
            {
                try
                {
                    string textOutput, errorOutput;
                    IEnumerable<object> objectOutput;

                    action(documentText, out textOutput, out errorOutput, out objectOutput);
                    Dispatcher.InvokeAsync(() =>
                    {
                        if (!string.IsNullOrEmpty(errorOutput))
                        {
                            if (CommandFailed != null)
                                CommandFailed(csharpCode, textOutput, errorOutput);
                        }
                        else
                        {
                            if (CommandExecuted != null)
                                CommandExecuted(csharpCode, textOutput, objectOutput);
                            Document.Text = "";
                        }

                        IsEnabled = true;
                        if (Executing != null)
                            Executing(false);
                    });
                }
                catch (ExitRequestedException)
                {
                    Dispatcher.InvokeAsync(() =>
                    {
                        if (CloseRequested != null)
                            CloseRequested();
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            });
        }