Start two or more process, one by one, real time output inside TextBox


I’m struggling with rather simple concept of the application which will:

-start two or more process from array, one by one, via Form Button Click
-display real time output inside TextBox

It’s easy doable by using c#. What’s the catch?

  • I need to use Powershell
  • I can’t use WaitOnExit because i need real time output
  • Sometimes app needs input so I need send it from another TextBox

This is my attempt to solve this by Runspaces:

  • ability to see process output
  • ability to send input if it will be required
  • if process execution code is triggered outside of the form then everything works as desired: multiple process are executed one by one
  • but as soon as I use the same code for Form Button2, the whole application hangs even if it’s inside runspace
  • removing loop from Button2_Click code unfreeze application but both process are executed at once

I need that last missing piece to have working solution. But I don’t quite know what could be the cause:

  • usage of the same thread?
  • how Powershell STA force things to be?
  • synchronization problem?

I know that it is not a easy-pezzy code to deal, but can someone with solid c#/Forms experience take look at it and see what is missing? Maybe it’s impossible to achieve via Powershell Forms due to Powershell STA?

You can simply run below code to see:

@AL|EN - Did you get the solution for this ? if so please share it with us which will be helpful for others else please update this thread with latest update.

@kvprasoon It’s very sophisticated problem, I resign from using Runspaces, simply use $process.SynchronizingObject = $Form1 and you can redirect Output, Error and Input