Powershell - 从开始工作更新文本框
Powershell - updating textbox from start-job
我一直在努力将我用 C# 编写的 WPF 程序转移到 PowerShell,但我在从 Start-Job 向我的 RichTextBox 中附加文本时遇到了一些问题,看起来我可以调用 Set -RichTextBox 在任何地方,除了在脚本块内。有解决方法吗?
#outside the ScriptBlock
Set-RichTextBox -message "This does work"
#outside the ScriptBlock
$job = Start-Job -ScriptBlock {
function Set-RichTextBox
{
param (
[System.String]
$message
)
$richTextBox1.Dispatcher.invoke([action]{
$richTextBox1.AppendText($message)
},"Normal")
}
Set-RichTextBox -message "This doesn't work"
}
我可以在除 Start-Job 之外的任何地方调用 RichTextBox,我没有收到任何错误消息。知道我在这里做错了什么吗?
对我来说它似乎有效:
Remove-Job *
$job = Start-Job -Name Test -ScriptBlock {
function Set-RichTextBox
{
param ([string]$message)
$message
}
Set-RichTextBox -message "This should work..."
}
Start-Sleep 5
Get-Job |Where-Object {$PSItem.Name -eq "Test"} |Receive-Job
你可能想看看:
PowerShell: Creating Custom Objects
还有这些可能相关也可能不相关:
- What's the best way to pass values to a running/background script block?
- Passing “native” object to background jobs
我一直在努力将我用 C# 编写的 WPF 程序转移到 PowerShell,但我在从 Start-Job 向我的 RichTextBox 中附加文本时遇到了一些问题,看起来我可以调用 Set -RichTextBox 在任何地方,除了在脚本块内。有解决方法吗?
#outside the ScriptBlock
Set-RichTextBox -message "This does work"
#outside the ScriptBlock
$job = Start-Job -ScriptBlock {
function Set-RichTextBox
{
param (
[System.String]
$message
)
$richTextBox1.Dispatcher.invoke([action]{
$richTextBox1.AppendText($message)
},"Normal")
}
Set-RichTextBox -message "This doesn't work"
}
我可以在除 Start-Job 之外的任何地方调用 RichTextBox,我没有收到任何错误消息。知道我在这里做错了什么吗?
对我来说它似乎有效:
Remove-Job *
$job = Start-Job -Name Test -ScriptBlock {
function Set-RichTextBox
{
param ([string]$message)
$message
}
Set-RichTextBox -message "This should work..."
}
Start-Sleep 5
Get-Job |Where-Object {$PSItem.Name -eq "Test"} |Receive-Job
你可能想看看: PowerShell: Creating Custom Objects
还有这些可能相关也可能不相关:
- What's the best way to pass values to a running/background script block?
- Passing “native” object to background jobs