public void ExecuteAsynchronously(string commandString) { PSListenerConsole psConsoleListener = new PSListenerConsole(PowerShellConsolePrinter); //Start the listener to listen for commands: try { m_PowershellConsoleListenerThread = new Thread(() => { psConsoleListener.WhenCommandIsDone += () => { this.WhenCommandIsDone(); }; psConsoleListener.Run(); }); m_PowershellConsoleListenerThread.IsBackground = true; m_PowershellConsoleListenerThread.Priority = ThreadPriority.BelowNormal; m_PowershellConsoleListenerThread.Start(); } catch (AggregateException ae) { foreach (var e in ae.InnerExceptions) { PowerShellConsolePrinter.WriteDelayedMessage("Error: " + e.Message.ToString() + "\n"); //Can be removed MessageBox.Show(e.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } try { psConsoleListener.ExecuteAsynchronously(commandString); //When the commands are considered done, notify! } catch (Exception e) { PowerShellConsolePrinter.WriteDelayedMessage("Error: " + e.Message.ToString() + "\n"); PowerShellConsolePrinter.SetPanelStatus("Error: " + e.Message.ToString(), Status.Failed); //Can be removed MessageBox.Show(e.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }