In C# when I want to create a thread which calls a function I simply do this.
Thread t = new Thread(() => calledFunction());
t.Start();
My goal is to do the same thing in Powershell. Nothing fancy, its only purpose is the prevent the GUI from freezing up. But i just cant find a working solution.
At the moment, I'm just trying to make the runspace to do anything. If I put this inside a click event of a button nothing happens.
$Runspace = [runspacefactory]::CreateRunspace()
$PowerShell = [powershell]::Create()
$PowerShell.runspace = $Runspace
$Runspace.Open()
[void]$PowerShell.AddScript({
$test = "test"
Write-Host $test
})
$PowerShell.Invoke()
But if I put a messagebox in the scriptblock then that works fine. It's very confusing to me when some things works and some things doesn't.
If you had a task where you had to call a function that does some work and updates a GUI. How would you go about calling that function in its own thread so that the GUI won't freeze while it's working in Powershell? In C# I have the easy example I posted above or using a BackgroundWorker.