不确定是否重复,查看了在线资料并与Boe Prox的解决方案合作,他在另一篇StackOverflow文章中提到了(https://dev59.com/e2_Xa4cB1Zd3GeqP5tqm#15502286),但是他是通过在线命令行/PowerShell窗口更新,通过在线程内运行的函数运行。我正在从一个按钮的事件中运行一个事件,位于线程内,并尝试运行单独的线程以进行点击事件。在线程外部,该事件正常工作,但在内部却无法工作。我做错了什么? PS. 我发现另一个博客引用了Boe Prox的工作(https://www.foxdeploy.com/blog/part-v-powershell-guis-responsive-apps-with-progress-bars.html),构建了另一个多线程应用程序,但基本上是相同的概念,通过放置在单独线程内的PowerShell命令集/函数更新窗口。
$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()
}
$comboBox.SelectedItem
是什么,2. 你使用同步哈希表初始化了运行空间,但没有使用它。 - Santiago Squarzon$syncHash
没有在你的脚本块中被使用。 - Santiago Squarzon$newRunspace.Open() $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) 因此,相同的代码被重复使用,在顶部(在我的程序中,我的代码上方)。 - user1546559