【问题标题】:Do Jobs really work in background in powershell?乔布斯真的在powershell后台工作吗?
【发布时间】:2016-01-05 21:01:07
【问题描述】:

我正在尝试实现在用户单击按钮后启动的后台命令,到目前为止,我的 UI 总是在工作进行时锁定。我目前正在使用一些耗时的 for 循环来检查我的 UI 是否锁定。当 UI 被释放时,我该怎么做才能让工作在后台工作。我不确定我是否想通过添加运行空间来使代码复杂化。我做错了什么?

$inputXML = @"
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="Create User" Height="448.05" Width="656.017" ResizeMode="NoResize">
    <Grid Margin="0,0,-6.8,-0.8" Background="#FFD7D7D7">
        <Grid.RowDefinitions>
            <RowDefinition Height="403*"/>
            <RowDefinition Height="18*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Button x:Name="Create_User" Content="Create User" HorizontalAlignment="Left" Margin="67,224,0,0" VerticalAlignment="Top" Width="300" Height="26" RenderTransformOrigin="0.551,-0.671" IsEnabled="True">
            <Button.Background>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FFF3F3F3" Offset="0"/>
                    <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
                    <GradientStop Color="#FFDDDDDD" Offset="0.5"/>
                    <GradientStop Color="#FFCDCDCD" Offset="1"/>
                </LinearGradientBrush>
            </Button.Background>
        </Button>
        <Label x:Name="fname" Content="" HorizontalAlignment="Left" Margin="43,11,0,0" VerticalAlignment="Top" Height="26" Width="10"/>
        <Label x:Name="fname1" Content="First Name" HorizontalAlignment="Left" Margin="88,54,0,0" VerticalAlignment="Top" Width="72" RenderTransformOrigin="0.513,1.469" Height="26"/>
        <Label x:Name="lname" Content="Last Name" HorizontalAlignment="Left" Margin="88,83,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.167,-0.458" Width="72" Height="25"/>
        <TextBox x:Name="fnametxt" Height="23" Margin="167,54,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="0.501,0.452" HorizontalAlignment="Left" Width="136"/>
        <Button x:Name="exitbtn" Content="Exit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="135" Margin="447,365,0,0" Height="38" RenderTransformOrigin="0.489,0.462"/>
        <Label x:Name="label" Content="Password" HorizontalAlignment="Left" Margin="92,113,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.064,0.601" Height="26" Width="68"/>
        <PasswordBox x:Name="passwordBox" Margin="167,113,0,0" VerticalAlignment="Top" Height="24" HorizontalAlignment="Left" Width="136"/>
        <TextBox x:Name="stsbox" HorizontalAlignment="Left" Height="62" Margin="67,267,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="300" Background="#FFDADADA" Foreground="Black" Opacity="0.45" SelectionBrush="#FF1D6EBF" RenderTransformOrigin="0.503,-0.59" IsReadOnly="True"/>
        <TextBox x:Name="lnametxt" Height="23" Margin="167,85,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Width="136"/>
    </Grid>
</Window>
"@ 

$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[xml]$xaml = $inputXML

#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -Scope Global}


Function global:Get-FormVariables{
if ($global:ReadmeDisplay -ne $true) {$global:ReadmeDisplay=$true}
#write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
} 


Get-FormVariables


 $WPFCreate_User.Add_Click({

$test = { for($i=1; $i -le 100000; $i++){
$z = $i + $z
$z
}}

$job = Start-Job $test 
Wait-Job $job

Receive-Job $job -OutVariable results
Remove-Job $job
$WPFstsbox.text = "$results`n"
                           }) 

 $WPFexitbtn.add_click({
 $form.Close() | out-null
 exit})

$form.ShowDialog() | Out-null

【问题讨论】:

  • “运行代码 sn-p”是怎么回事?
  • @JaquelineVanek 他们在编辑器中选择了错误的按钮,为他们添加了该按钮。

标签: powershell jobs


【解决方案1】:

是的,PSJobs 是真正的“后台”工作。

当您调用Start-Job 时,将启动一个独立进程,执行您的作业/命令。

在您的脚本中,作业本身不会阻塞调用线程,但您的后续命令 (Wait-Job $job) 会阻塞。


如果您只是触发 Start-Job 并从 Click 处理程序返回,您的 UI 不会锁定:

$button.add_Click({
  $jobCode = { Start-Sleep -Seconds 10 }
  $job = Start-Job $jobCode
})

您会看到 UI 在不到 10 秒后再次响应。

问题是您无法再访问$job,也无法控制它何时显示。

为了弥补这一点,您需要后台线程或定时事件来定期为您检查作业并输出结果。

您可以为此使用 Timer 和 PowerShell 的内置事件基础架构:

# Create timer
$backgroundTimer = New-Object System.Timers.Timer
# Set interval (ms)
$backgroundTimer.Interval = 500
# Make sure timer stops raising events after each interval
$backgroundTimer.AutoReset = $false

# Have powershell "listen" for the event in the background
Register-ObjectEvent $backgroundTimer -EventName 'Elapsed' -SourceIdentifier 'timerElapsed' -Action {

  # Loop through any completed jobs with data to return, from "oldest" to "newest"
  while(($finishedJob = Get-Job |Where-Object {$_.State -eq 'Completed' -and $_.HasMoreData}|Select-Object -First 1))
  {
    # Update the text box on your WPF form with the results
    # NOTE: the event is executed in a separate scope, thus the "global:" scope prefix
    $global:WPFstsbox.Text = "$(Receive-Job $finishedJob)`n"

    # Clean up the job
    Remove-Job $finishedJob
  }

  # Restart timer
  $backgroundTimer.Start()
}

# Enable the timer
$backgroundTimer.Enabled = $true

【讨论】:

  • 感谢您指出这一点,我真的是想弄清楚这一点。在使用后台线程的情况下,我尝试使用你的方法,它似乎吐了一个Cannot Subscribe to specified event. A subscriber with the source identifier 'timerElapsed' already exists. 错误。
  • 原谅第二条评论,无法编辑我之前的评论。似乎在我运行您的代码之后,SourceIdentifier 仍然存在于后台,因此当我重新运行代码时,powershell 会吐出错误,指出源标识符已经存在。我在作业完成后考虑使用 Unregister-Event,但是我对 cmdlet 没有任何运气。至于现在,我每次重新运行代码时都必须分配一个新的 SourceIdentifier 属性,并且它可以按我的意愿工作!我会尝试找出如何摆脱我之前提到的错误
  • @Bregs 正确,您需要在关闭 WPF 应用程序时手动清理事件注册。对不起,我应该提到这一点。为此使用Unregister-Event
  • 我遇到了一个奇怪的问题,当我在 ISE 中工作时,我可以通过单击按钮来填充字段,但是从 powershell 控制台运行脚本需要我单击按钮两次在结果被填充之前。有没有解决的办法?我所做的只是添加了Unregister-Event cmdlet。
  • 您在哪里添加了Unregister-Event?我的答案中的代码是 BEFORE 和 Unregister-Event AFTER $form.ShowDialog()
猜你喜欢
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 2013-03-23
  • 1970-01-01
相关资源
最近更新 更多