winutil/functions/public/Invoke-WPFRunspace.ps1
Carter acc2b5b243 Comment Spacing, Indentation, and Capitalization (#1084)
* Comment Spacing, Indentation, and Capitalization

* Comment Grammar and Spacing

Makes grammar in comments better and more consistent
Adds space before comment and centers word in `Write-Host` commands

* More Grammar and Formatting

* Add some comments

* Populate PlaceHolder comments in functions

Files I found that has issues:
Get-WinUtilRegistry.ps1
Install-WinUtilWinget.ps1
Invoke-WinUtilDarkMode.ps1
Remove-WinUtilAPPX.ps1
Test-WinUtilPackageManager.ps1
Update-WinUtilProgramWinget.ps1

Invoke-WPFUpdatessecurity.ps1

* Tweak a few more comments

* Tweak another write-host statement

* Undo Catch statement adjustment

It's outside of the scope of this pull request
2024-01-26 11:02:50 -06:00

47 lines
1.3 KiB
PowerShell

function Invoke-WPFRunspace {
<#
.SYNOPSIS
Creates and invokes a runspace using the given scriptblock and argumentlist
.PARAMETER ScriptBlock
The scriptblock to invoke in the runspace
.PARAMETER ArgumentList
A list of arguments to pass to the runspace
.EXAMPLE
Invoke-WPFRunspace `
-ScriptBlock $sync.ScriptsInstallPrograms `
-ArgumentList "Installadvancedip,Installbitwarden" `
#>
[CmdletBinding()]
Param (
$ScriptBlock,
$ArgumentList
)
# Create a PowerShell instance
$script:powershell = [powershell]::Create()
# Add Scriptblock and Arguments to runspace
$script:powershell.AddScript($ScriptBlock)
$script:powershell.AddArgument($ArgumentList)
$script:powershell.RunspacePool = $sync.runspace
# Execute the RunspacePool
$script:handle = $script:powershell.BeginInvoke()
# Clean up the RunspacePool threads when they are complete, and invoke the garbage collector to clean up the memory
if ($script:handle.IsCompleted)
{
$script:powershell.EndInvoke($script:handle)
$script:powershell.Dispose()
$sync.runspace.Dispose()
$sync.runspace.Close()
[System.GC]::Collect()
}
}