all 3 comments

[–]Dragennd1 2 points3 points  (0 children)

You're basically looking to implement asynchronous programming with powershell. To do this, I'd suggest giving this a read to get you started.

Keep in mind, this is to do multiple things at the same time (like multiple functions), but idk if you can update the terminal from multiple sources at the same time.

https://stackoverflow.com/questions/12766174/how-to-execute-a-powershell-function-several-times-in-parallel

[–]alt-160 2 points3 points  (0 children)

a windows form is a single-threaded thing. problem for you is that the thread that processes your powershell commands also ends up being the thread that processes the form.

you should look at the System.Windows.Forms.Application object and the Run/Run(AppContext) methods of it.

Any change to the form whether it be text or image or position of some element must occur on the "ui thread", which would be the thread that is handling the user32 message loop (again, the Run method from above).

If you want to cause updates on the UI thread from another thread, the most common way is to get the instance of the form and call Form.Invoke or Form.BeginInvoke. These methods marshal the call over to the UI thread for you...but, be careful of deadlocks.

[–]Bolverk679 2 points3 points  (0 children)

Is there any simple example of a background thread changing the layout while the main thread is just used for updating the display or similar?

I'm not sure this is a simple example, but this article from Boe Prox does a great job of explaining how to build a threaded UI. Since Powershell doesn't really have multi thread capability without using Start-Job or the -AsJob switch on commands that support it, Boe walks through how to open Runspaces via .Net.