我有一种表单,可以在其中立即添加几个元素(例如,列表)。添加它们可能需要一些时间(从几分之一秒到几分钟)。因此,我想将处理添加到单独的线程(子线程)中。元素的数量事先未知(例如,文件夹中有多少个文件),因此会在子流中创建它们。当子流中的处理结束时,我想在主表单上显示这些元素(在表单没有这些元素并执行其他任务之前)。
但是,我面对这样一个事实,我无法将这些元素从子流添加到主表单中。我将举一个简单的例子作为例子。它当然有效:
$Main = New-Object System.Windows.Forms.Form
$Run = {
# The form is busy while adding elements (buttons here)
$Top = 0
1..5 | % {
$Button = New-Object System.Windows.Forms.Button
$Button.Top = $Top
$Main.Controls.Add($Button)
$Top += 30
Sleep 1
}
}
$Main.Add_Shown($Run)
# Adding and performing other tasks on the form here
[void]$Main.ShowDialog()
但是,将相同的内容添加到子流中后,我没有在主窗体上显示该按钮。我不理解为什么。
$Main = New-Object System.Windows.Forms.Form
$Run = {
$RS = [Runspacefactory]::CreateRunspace()
$RS.Open()
$RS.SessionStateProxy.SetVariable('Main', $Main)
$PS = [PowerShell]::Create().AddScript({
# Many items will be added here. Their number and processing time are unknown in advance
# Now an example with the addition of five buttons.
$Top = 0
1..5 | % {
$Button = New-Object System.Windows.Forms.Button
$Button.Top = $Top
$Main.Controls.Add($Button)
$Top += 30
Sleep 1
}
})
$PS.Runspace = $RS; $Null = $PS.BeginInvoke()
}
$Main.Add_Shown($Run)
[void]$Main.ShowDialog()
如何在子流中创建的主表单中添加元素?谢谢
最佳答案
虽然可以在线程B上创建控件,但是不能将它们添加到从线程B 在线程A中创建的控件中。
如果尝试这样做,将出现以下异常:
Controls created on one thread cannot be parented to a control on a different thread.
育儿意味着在控件(窗体)上调用
.Add()
或.AddRange()
方法以将其他控件添加为子控件。换句话说:要将控件添加到
$Main
表单中,该表单已创建并随后显示在原始线程(PowerShell运行空间)中,因此$Main.Controls.Add()
调用必须在同一线程中进行。 同样,您也应始终在同一线程中附加事件委托(delegate)(事件处理程序脚本块)。
尽管your own answer尝试确保将按钮添加到原始运行空间中的表单中,但它不能按书面形式工作-请参阅底部。
我建议一种更简单的方法:
Start-ThreadJob
在后台创建控件。Start-ThreadJob
是 ThreadJob
module的一部分,它为基于子进程的常规后台作业提供了基于线程的轻量级替代方案,并且是通过PowerShell SDK创建运行空间的更便捷替代方案。它随PowerShell [Core] v6 +一起提供,在Windows中可以按需安装PowerShell,例如
Install-Module ThreadJob -Scope CurrentUser
。在大多数情况下,就性能和类型保真度而言,线程作业是更好的选择-有关原因,请参见this answer的底部。
.Show()
而不是.ShowDialog()
),并在[System.Windows.Forms.Application]::DoEvents()
循环中处理GUI事件。[System.Windows.Forms.Application]::DoEvents()
可能会出现问题(本质上是阻塞的.ShowDialog()
调用在后台执行的操作),但是在这种受限的情况下(假设仅显示一种形式),应该没问题。有关背景信息,请参见this answer。 这是一个工作示例,在使窗体可见后在窗体中添加了3个按钮,在它们之间休眠时一个接一个地添加了三个按钮:
Add-Type -ea Stop -Assembly System.Windows.Forms
$Main = New-Object System.Windows.Forms.Form
# Start a thread job that will create the buttons.
$job = Start-ThreadJob {
$top = 0
1..3 | % {
# Create and output a button object.
($btn = [System.Windows.Forms.Button] @{
Name = "Button$_"
Text = "Button$_"
Top = $top
})
Start-Sleep 1
$top += $btn.Height
}
}
# Show the form asynchronously
$Main.Show()
# Process GUI events in a loop, and add
# buttons to the form as they're being created
# by the thread job.
while ($Main.Visible) {
[System.Windows.Forms.Application]::DoEvents()
if ($button = Receive-Job -Job $job) {
# Add an event handler...
$button.add_Click({ Write-Host "Button clicked: $($this.Name)" })
# .. and it to the form.
$Main.Controls.AddRange($button)
}
}
# Clean up.
$Main.Dispose()
Remove-Job -Job $job -Force
'Done'
在撰写本文时,鉴于用于事件处理的
Register-ObjectEvent
脚本块正在运行(在内部的动态模块中),因此your own answer尝试通过使用-Action
预订其他线程(运行空间)的事件来将控件添加到原始运行空间中的表单中。 )原始线程(运行空间),但是有两个问题:-Action
脚本块既不能直接从原始运行空间中看到$Main
变量,也不能从其他运行空间的变量中看到-但是,可以通过将$Main
通过Register-ObjectEvent
传递给-MessageData
并通过脚本中的$Event.MessageData
访问它来解决这些问题。块,并通过$Sender.Runspace.SessionStateProxy.GetVariable()
调用访问其他运行空间的变量。 .ShowDialog()
调用将阻止进一步的处理。也就是说,您的事件不会触发,因此,在表单关闭之前,不会调用-Action
脚本块。MouseMove
事件,该事件处理程序的调用使PowerShell有机会在以模态显示表单时触发自己的事件;例如:$Main.Add_MouseMove({ Out-Host })
;请注意,仅当脚本块有效时,此解决方法才有效调用一个命令,例如本示例中的
Out-Host
(实际上是无操作);仅表达式或.NET方法调用是不够的。 关于winforms - 从另一个运行空间向表单添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60127351/