Not sure if this is a duplicate, checked online, and worked with what I found, Working with Boe Prox's solutions, which from another StackOverflow article references (https://stackoverflow.com/a/15502286/1546559), but in his, he is updating from a command line/powershell window, via a function run inside a thread. I'm running an event from a button, inside of a thread and trying to run a separate thread, for the click event(s). Outside of the thread, the event works fine, but inside, it doesn't work, at all.. What am I doing wrong? PS. I found another blog referencing Boe Prox's work (https://www.foxdeploy.com/blog/part-v-powershell-guis-responsive-apps-with-progress-bars.html), building another multi-threaded application, but pretty much the same concept, updating a window, through powershell commandlet/function, placed inside of a separate thread.
$push.Add_Click{
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$powershell = [powershell]::Create().AddScript({
$choice = $comboBox.SelectedItem
# $drive = Get-Location
if(!(Test-Path -PathType Container -Path "L:\$choice"))
{
# New-Item -ItemType "container" -Path . -Name $choice
New-Item -ItemType "Directory" -Path . -Name $choice
}
# $folder = $_
# Where is it being stored at?
[System.IO.File]::ReadLines("Y:\$choice\IPs.txt") | foreach {
ping -a -n 2 -w 2000 $_ | Out-Null
Test-Connection -Count 2 -TimeToLive 2 $_ | Out-Null
if($?)
{
RoboCopy /Log:"L:\$folder\$_.log" $source \\$_\c$\tools
RoboCopy /Log+:"L:\$folder\$folder-MovementLogs.log" $source \\$_\c$\tools
Start-Process "P:\psexec.exe" -ArgumentList "\\$_ -d -e -h -s cmd /c reg import C:\tools\dump.reg"
# Copy-Item -LiteralPath Y:\* -Destination \\$_\c$\tools
$listBox.Items.Add($_)
}
}
})
$powershell.Runspace = $newRunspace
$powershell.BeginInvoke()
}