在Powershell中释放Runspace(GUI特定)

5
我是StackOverflow的积极读者,因为它通常可以帮助我解决所有问题。但是对于我第一个问题,我将向您寻求有关Powershell中runspaces的帮助,特别是内存管理方面。
我创建了一些带GUI的PS应用程序,现在我使用runspaces来避免在运行大型操作时挂起GUI的情况。我的问题是当runspaces完成后,我无法处理它们。
关于下面的代码,这是一个简单的脚本,旨在尝试了解如何处理我的runspaces。我尝试将唯一的runspace合并到监视和处理应用程序runspaces中,我受到proxb's script的启发(顺便说一下,我是他的粉丝),但似乎不起作用。
每次执行runspace时,我会获得10Mb的内存,造成巨大的泄漏!

leak

你能帮我解决这个问题吗?

[xml]$xaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Runspace" FontFamily="Calibri" Height="400" Width="400" FontSize="14">
    <Grid Margin="10,10,10,10">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBox x:Name="TB_Results" Grid.Row="0" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
        <Button x:Name="BT_Run" Grid.Row="2" Content="Run" Height="30"/>
    </Grid>
</Window>
'@

# Inits
Add-Type -AssemblyName PresentationFramework            
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
$gui = [hashtable]::Synchronized(@{})
$reader = (New-Object Xml.XmlNodeReader $xaml)
$gui.Window = [Windows.Markup.XamlReader]::Load($reader)
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object { $gui.Add($_.Name,$gui.Window.FindName($_.Name)) }
$Script:Jobs = [system.collections.arraylist]::Synchronized((New-Object System.Collections.ArrayList))
$Script:JobCleanup = [hashtable]::Synchronized(@{ Host = $host })

# Test function
function RunFunc {
        $newRunspace = [runspacefactory]::CreateRunspace()
        $newRunspace.ApartmentState = "STA"
        $newRunspace.ThreadOptions = "ReuseThread"
        $newRunspace.Open()
        $newRunspace.SessionStateProxy.SetVariable("gui",$gui)
        $Code = {
            $memupdate = "Memory: $($(Get-Process -Id $PID).PrivateMemorySize64 / 1mb) mb"
            $gui.Window.Dispatcher.invoke("Normal",[action]{
                $gui.TB_Results.Text = "$memupdate`r`n" + $gui.TB_Results.Text
            })
        }
        $Powershell = [powershell]::Create().AddScript($Code)
        $Powershell.Runspace = $newRunspace
        [void]$Script:Jobs.Add((
            [PSCustomObject]@{
                PowerShell = $PowerShell
                Runspace = $PowerShell.BeginInvoke()
            }
        ))
}

# Background Runspace to clean up jobs
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"          
$newRunspace.Open() 
$newRunspace.SessionStateProxy.SetVariable("jobs",$Script:Jobs) 
$Code = {
    $JobCleanup = $true
    do {    
        foreach($runspace in $jobs) {            
            if ($runspace.Runspace.isCompleted) {
                $runspace.powershell.EndInvoke($runspace.Runspace) | Out-Null
                $runspace.powershell.dispose()
                $runspace.Runspace = $null
                $runspace.powershell = $null               
            } 
        }
        $temphash = $jobs.clone()
        $temphash | Where-Object { $_.runspace -eq $Null } | ForEach-Object { $jobs.remove($_) }        
        Start-Sleep -Seconds 1     
    } while ($JobCleanup)
}
$Powershell = [powershell]::Create().AddScript($Code)
$Powershell.Runspace = $newRunspace
$PowerShell.BeginInvoke()

# gui events
$gui.BT_Run.add_Click({ RunFunc })
$gui.Window.ShowDialog() | Out-Null
1个回答

5

当您调用$runspace.PowerShell.Dispose()时,PowerShell实例将被处理,但它所绑定的运行空间不会自动处理,您需要在清理任务中首先自己处理它:

$runspace.powershell.EndInvoke($runspace.Runspace) | Out-Null
$runspace.powershell.Runspace.Dispose() # remember to clean up the runspace!
$runspace.powershell.dispose()

2
@Carlton2001 很高兴听到这个消息!出于你自己的利益,我建议将自定义作业对象中的Runspace属性重命名为 ResultHandle,然后将foreach($runspace in $jobs)更改为 foreach($job in $jobs) - Mathias R. Jessen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接