/// <summary> /// Shows the window. /// </summary> /// <param name="initializer">Action that will initialize <see cref="InteractiveWindow"/> before showing it.</param> public static void ShowWindow(Action <InteractiveWindow> initializer = null) { System.Threading.AutoResetEvent windowShown = new System.Threading.AutoResetEvent(false); ExecuteInSTA(() => { InteractiveWindow window = null; try { window = new InteractiveWindow(); initializer?.Invoke(window); window.Show(); windowShown.Set(); var _dispatcherFrame = new System.Windows.Threading.DispatcherFrame(); window.Closed += (obj, e) => { _dispatcherFrame.Continue = false; }; System.Windows.Threading.Dispatcher.PushFrame(_dispatcherFrame); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { windowShown.Set(); } window?.Close(); System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeShutdown(); }, waitForExecution: false); windowShown.WaitOne(); }
/// <summary> /// Shows the window as modal dialog. /// </summary> /// <param name="initializer">Action that will initialize <see cref="InteractiveWindow"/> before showing it.</param> public static void ShowModalWindow(Action <InteractiveWindow> initializer = null) { ExecuteInSTA(() => { InteractiveWindow window = null; try { window = new InteractiveWindow(); initializer?.Invoke(window); window.ShowDialog(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } window.Close(); System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeShutdown(); }); }
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); }