XAML Powershell GUI冻结,即使使用运行空间

im9ewurl  于 2023-06-19  发布在  Shell
关注(0)|答案(2)|浏览(150)

我正在创建一个带有GUI的powershell脚本,该脚本将用户配置文件从选定的源磁盘复制到目标磁盘。我用XAML创建了GUI,使用VS Community 2019。脚本的工作方式如下:选择源磁盘、目标磁盘、用户配置文件和要复制的文件夹。当您按下按钮“Start”时,它调用一个名为Backup_data的函数,在此创建一个运行空间。在这个运行空间中,只有一个小小的**Copy-Item,**以您选择的内容作为参数。
脚本工作正常,所有想要的项目都被正确复制。问题是GUI在复制过程中被冻结了(没有“不响应”的消息或其他什么,它只是完全冻结了;不能点击任何地方,不能移动窗口)。我已经看到使用运行空间可以解决这个问题,但对我来说不是。我错过了什么吗?
下面是函数Backup_Data

Function BackupData {  
  ##CREATE RUNSPACE
  $PowerShell = [powershell]::Create()
  [void]$PowerShell.AddScript( {
      Param ($global:ReturnedDiskSource, $global:SelectedUser, $global:SelectedFolders, $global:ReturnedDiskDestination)
      ##SCRIPT BLOCK
      foreach ($item in $global:SelectedFolders) {
        Copy-Item -Path "$global:ReturnedDiskSource\Users\$global:SelectedUser\$item" -Destination "$global:ReturnedDiskDestination\Users\$global:SelectedUser\$item" -Force -Recurse
      }
    }).AddArgument($global:ReturnedDiskSource).AddArgument($global:SelectedUser).AddArgument($global:SelectedFolders).AddArgument($global:ReturnedDiskDestination)
  #Invoke the command
  $PowerShell.Invoke()
  $PowerShell.Dispose()
}
jhdbpxl9

jhdbpxl91#

PowerShell SDK的**PowerShell.Invoke()方法是 * 同步的***,因此通过设计,当其他运行空间(线程)中的脚本运行时,它会 * 阻塞 *。
必须使用异步PowerShell.BeginInvoke()方法

    • 简单示例 * 图片中没有WPF***(WPF解决方案见底部):
$ps = [powershell]::Create()

# Add the script and invoke it *asynchronously*
$asyncResult = $ps.AddScript({ Start-Sleep 3; 'done' }).BeginInvoke()

# Wait in a loop and check periodically if the script has completed.
Write-Host -NoNewline 'Doing other things..'
while (-not $asyncResult.IsCompleted) {
  Write-Host -NoNewline .
  Start-Sleep 1
}
Write-Host

# Get the script's success output.
"result: " + $ps.EndInvoke($asyncResult)

$ps.Dispose()

请注意,使用PowerShell SDK有一个更简单的替代方法ThreadJob模块的**Start-ThreadJob**cmdlet,一个基于 * thread * 的替代方案,用于替代以Start-Job启动的基于 * child-process * 的常规后台作业,它与所有其他*-Job cmdlet兼容。
Start-ThreadJob附带 * PowerShell [Core] 7 +*,可从 * Windows PowerShell * 中的PowerShell GalleryInstall-Module ThreadJob)安装。

# Requires module ThreadJob (preinstalled in v6+)

# Start the thread job, always asynchronously.
$threadJob = Start-ThreadJob { Start-Sleep 3; 'done' }

# Wait in a loop and check periodically if the job has terminated.
Write-Host -NoNewline 'Doing other things..'
while ($threadJob.State -notin 'Completed', 'Failed') {
  Write-Host -NoNewline .
  Start-Sleep 1
}
Write-Host

# Get the job's success output.
"result: " + ($threadJob | Receive-Job -Wait -AutoRemoveJob)
    • 完整示例 ,带 * WPF:*

如果代码需要从附加到WPF窗口中的控件的事件处理程序运行,则需要更多的工作,因为**Start-Sleep不能 * 使用,因为它 * 阻止GUI事件的处理 * 并因此冻结窗口。

下面的例子:

  • 创建一个带有两个后台操作启动按钮和相应状态文本框的窗口。
  • 使用按钮单击事件处理程序通过Start-ThreadJob启动后台操作:
  • 注意:Start-Job也可以工作,但这将在一个 * 子进程 * 而不是线程中运行代码,这要慢得多,并有其他重要的后果。
  • 让这个例子适应PowerShell SDK([powershell])的使用也并不难,但是线程作业更符合PowerShell习惯,并且更容易通过常规的*-Job cmdlet进行管理。
  • 以 * 非 * 模式显示WPF窗口并进入 * 自定义 * 事件循环:
  • 一个 * custom * DoEvents()类似的函数DoWpfEvents,它是从DispatcherFrame documentation改编而来的,在GUI事件处理的每个循环操作中都被调用。
  • 注意:对于 * WinForms * 代码,您可以简单地调用[System.Windows.Forms.Application]::DoEvents()-事实上,如果您不介意同时加载 * WPF和WinForms程序集,您可以 * 直接 * 使用后者。
  • 此外,还监视后台线程作业的进度,并将接收到的输出附加到作业特定状态文本框中。已完成的作业将被清理。
    • 注意**:就像你用.ShowModal()方式调用窗口一样,前台线程和控制台会话在窗口显示时会被阻塞。避免这种情况的最简单方法是改为在隐藏的子进程中运行代码;假设代码在脚本wpfDemo.ps1中:
# In PowerShell [Core] 7+, use `pwsh` instead of `powershell`
Start-Process -WindowStyle Hidden powershell '-noprofile -file wpfDemo.ps1'

你也可以通过SDK来实现,这样会更快,但它更冗长和麻烦:
$runspace = [runspacefactory]::CreateRunspace() $runspace.ApartmentState = 'STA'; $runspace.Open(); $ps = [powershell]::Create(); $ps.Runspace = $runspace; $null = $ps.AddScript((Get-Content -Raw wpfDemo.ps1)).BeginInvoke()

    • 截图**:

此示例屏幕截图显示了一个已完成的后台操作和一个正在进行的操作(支持并行运行它们);注意启动正在进行的操作的按钮如何在操作期间被禁用,以防止重新输入:

    • 源码:**
using namespace System.Windows
using namespace System.Windows.Threading

# Load WPF assemblies.
Add-Type -AssemblyName PresentationCore, PresentationFramework

# Define the XAML document, containing a pair of background-operation-launching
# buttons plus associated status text boxes.
[xml] $xaml = @"
<Window
        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:Test"
        Title="MainWindow" Height="220" Width="600">
    <Grid>
        <TextBox x:Name="Status1" Height="140" Width="280" Margin="10,10" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" AcceptsReturn="True" AcceptsTab="True" Padding="4" VerticalScrollBarVisibility="Auto" />
        <TextBox x:Name="Status2" Height="140" Width="280" Margin="10,10" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Right" AcceptsReturn="True" AcceptsTab="True" Padding="4" VerticalScrollBarVisibility="Auto" />
        <Button x:Name="DoThing1" Content="Do Thing 1" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="100" Height="22" Margin="10,5" IsDefault="True" />
        <Button x:Name="DoThing2" Content="Do Thing 2" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="100" Height="22" Margin="10,5" />
    </Grid>
</Window>
"@

# Parse the XAML, which returns a [System.Windows.Window] instance.
$Window = [Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml))

# Save the window's relevant controls in PowerShell variables.
# Background-operation-launching buttons.
$btns = $Window.FindName('DoThing1'), $Window.FindName('DoThing2')

# Use a [hashtable] to map the buttons to the associated status text boxes.
$txtBoxes = @{
  $btns[0] = $Window.FindName('Status1')
  $btns[1] = $Window.FindName('Status2')
}
# Use a [hashtable] to map the buttons to the associated background
# operations, defined as script blocks to be passed to Start-ThreadJob later.
# The sample operations here run for a few seconds, 
# emitting '.' every second and a message on completion.
$scriptBlocks = @{
  $btns[0] = 
    {
      1..3 | ForEach-Object { '.'; Start-Sleep 1 }
      'Thing 1 is done.'
    }
  $btns[1] = 
    {
      1..2 | ForEach-Object { '.'; Start-Sleep 1 }
      'Thing 2 is done.'
    }
}

# Attach the button-click event handlers that
# launch the background operations (thread jobs).
foreach ($btn in $btns) {

  $btn.Add_Click({

    # Temporarily disable this button to prevent re-entry.
    $this.IsEnabled = $false

    # Show a status message in the associated text box.
    $txtBoxes[$this].Text = "Started thing $($this.Name -replace '\D') at $(Get-Date -Format T)."

    # Asynchronously start a background thread job named for this button.
    # Note: Would work with Start-Job too, but that runs the code in *child process*, 
    #       which is much slower and has other implications.
    $null = Start-ThreadJob -Name $this.Name $scriptBlocks[$this]

  })

}

# Define a custom DoEvents()-like function that processes GUI WPF events and can be 
# called in a custom event loop in the foreground thread.
# Adapted from: https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcherframe
function DoWpfEvents {
  [DispatcherFrame] $frame = [DispatcherFrame]::new($True)
  $null = [Dispatcher]::CurrentDispatcher.BeginInvoke(
    'Background', 
    [DispatcherOperationCallback] {
      param([object] $f)
      ($f -as [DispatcherFrame]).Continue = $false
      return $null
    }, 
    $frame)
  [Dispatcher]::PushFrame($frame)
}

# Finally, display the window NON-modally...
$Window.Show() 
$null = $Window.Activate() # Ensures that the window gets the focus.
# ... and enter a custom event loop based on calling the custom .DoEvents() method
while ($Window.IsVisible) {

  # Process GUI events.
  DoWpfEvents

  # Process pending background (thread) jobs, if any.
  Get-Job | ForEach-Object {
    
    # Get the originating button via the job name.
    $btn = $Window.FindName($_.Name)
    # Get the corresponding status text box.
    $txtBox = $txtBoxes[$btn]

    # Test if the job has terminated.
    $completed = $_.State -in 'Completed', 'Failed', 'Stopped'

    # Append any new results to the respective status text boxes.
    # Note the use of redirection *>&1 to capture ALL streams, notably including the error stream.
    if ($data = Receive-Job $_ *>&1) {
      $txtBox.Text += "`n" + ($data -join "`n")
    }

    # Clean up, if the job is completed.
    if ($completed) {
      Remove-Job $_
      $btn.IsEnabled = $true # re-enable the button.
      $txtBox.Text += "`nJob terminated on: $(Get-Date -Format T); status: $($_.State)."
    }

  }

  # Note: If there are no GUI events pending, this loop will cycle very rapidly.
  #       To mitigate this, we *also* sleep a little, but short enough to 
  #       still keep the GUI responsive.
  # IMPORTANT: Do NOT use Start-Sleep, as certain events 
  #            - notably reactivating a minimized window from the taskbar - 
  #            then do not work.
  [Threading.Thread]::Sleep(50)

}

# Window was closed; clean up:
# If the window was closed before all jobs completed, 
# get the incomplete jobs' remaining output, wait for them to finish, and delete them.
Get-Job | Receive-Job -Wait -AutoRemoveJob
hsvhsicv

hsvhsicv2#

我一整天都在寻找一个解决方案,我终于找到了一个,所以我打算把它贴在那里,给那些有同样问题的人。
首先,查看这篇文章:https://smsagent.blog/2015/09/07/powershell-tip-utilizing-runspaces-for-responsive-wpf-gui-applications/它解释得很好,并向您展示了如何在WPF GUI中正确使用运行空间。你只需要将$Window变量替换为$Synchhash。Window:

$syncHash = [hashtable]::Synchronized(@{})
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.window = [Windows.Markup.XamlReader]::Load( $reader )

在代码中插入运行空间函数:

function RunspaceBackupData {
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$Runspace.SessionStateProxy.SetVariable("SelectedFolders",$global:SelectedFolders)
$Runspace.SessionStateProxy.SetVariable("SelectedUser",$global:SelectedUser)
$Runspace.SessionStateProxy.SetVariable("ReturnedDiskSource",$global:ReturnedDiskSource)
$Runspace.SessionStateProxy.SetVariable("ReturnedDiskDestination",$global:ReturnedDiskDestination)
$code = {
    foreach ($item in $global:SelectedFolders) {
        copy-item -Path "$global:ReturnedDiskSource\Users\$global:SelectedUser\$item" -Destination "$global:ReturnedDiskDestination\Users\$global:SelectedUser\$item" -Force -Recurse
        }
}
$PSinstance = [powershell]::Create().AddScript($Code)
$PSinstance.Runspace = $Runspace
$job = $PSinstance.BeginInvoke()
}

并在所需的事件处理程序中使用您指定的参数调用它:

$var_btnStart.Add_Click( {
    RunspaceBackupData -syncHash $syncHash -SelectedFolders $global:SelectedFolders -SelectedUser $global:SelectedUser -ReturnedDiskSource $global:ReturnedDiskSource -ReturnedDiskDestination $global:ReturnedDiskDestination 
})

不要忘记结束你的运行空间:

$syncHash.window.ShowDialog()
$Runspace.Close()
$Runspace.Dispose()

相关问题