async-test.ps1

function Show-AnyBoxAsync
{
    [cmdletbinding()]
    param(
        [ValidateSet($null, 'Information', 'Warning', 'Error', 'Question')]
        [string]$Icon,
        [string]$Title,
        [string]$Image,
        [string[]]$Message,
        [AnyBox.Prompt[]]$Prompt,
        [string[]]$Buttons,
        [string]$CancelButton,
        [string]$DefaultButton,
        [switch]$ShowCopyButton,
        [ValidateScript({$_ -gt 0})]
        [uint16]$ButtonRows = 1,
        [string[]]$Comment,
        [ValidateSet('Left', 'Center')]
        [string]$ContentAlignment = 'Center',

        [ValidateNotNullOrEmpty()]
        [string]$FontFamily = 'Segoe UI',
        [ValidateScript({$_ -gt 0})]
        [uint16]$FontSize = 13,
        [ValidateNotNullOrEmpty()]
        [string]$FontColor = 'Black',
        [string]$BackgroundColor,
        [ValidateSet('None', 'SingleBorderWindow', 'ThreeDBorderWindow', 'ToolWindow')]
        [System.Windows.WindowStyle]$WindowStyle = 'SingleBorderWindow',
        [ValidateSet('Normal', 'Maximized')]
        [System.Windows.WindowState]$WindowState = 'Normal',
        [ValidateSet('NoResize', 'CanMinimize', 'CanResize', 'CanResizeWithGrip')]
        [System.Windows.ResizeMode]$ResizeMode = 'CanMinimize',
        [switch]$NoResize,
        [ValidateScript({$_ -gt 0})]
        [uint16]$MinHeight = 50,
        [ValidateScript({$_ -gt 0})]
        [uint16]$MinWidth = 50,
        [switch]$Topmost,
        [switch]$HideTaskbarIcon,
        [uint32]$Timeout,
        [switch]$Countdown,
        [System.Windows.Window]$ParentWindow = $null,

        [Parameter(ValueFromPipeline = $true)]
        [array]$GridData,
        [ValidateSet('SingleCell', 'MultiCell', 'SingleRow', 'MultiRow')]
        [string]$SelectionMode = 'SingleCell',
        [switch]$HideGridSearch,
        [switch]$GridAsList
    )

    begin {
        Write-Verbose 'Entered BEGIN'

        $form = [hashtable]::Synchronized(@{})

        if ($PSCmdlet.MyInvocation.ExpectingInput) {
            $form.Add('GridData', (New-Object System.Collections.ArrayList))
            $form.Add('LiveGrid', $true)
        }
        elseif ($GridData) {
            $form.Add('GridData', (New-Object System.Collections.ArrayList @(,$GridData)))
            $form.Add('LiveGrid', $true)
        }

        $runspace = [runspacefactory]::CreateRunspace()
        $runspace.ApartmentState = "STA"
        $runspace.ThreadOptions = "ReuseThread"         
        $runspace.Open()
        $runspace.SessionStateProxy.SetVariable("form", $form)

        $ps = [PowerShell]::Create()

        $null = $ps.AddScript((Get-Command 'Show-AnyBox').Definition)

        $null = $ps.AddParameters($PSBoundParameters)
        # $PSBoundParameters.Keys | foreach {
        # $null = $ps.AddParameter($_, $PSBoundParameters[$_])
        # }

        $ps.Runspace = $runspace
        $handle = $ps.BeginInvoke()
        
        do {
            Start-Sleep 0.1
        } while (-not $form.ContainsKey('Window'))

        [scriptblock]$cleanup = {
            param([bool]$force)

            Write-Verbose 'Entered END'
    
            Start-Sleep -Seconds 1
    
            $form['LiveGrid'] = $false

            if (-not $force) {
                $null = $handle.AsyncWaitHandle.WaitOne()
            }
    
            $form['GridData'].Clear()
            $form.Clear()
            $form = $null
    
            $psResult = $ps.EndInvoke($handle).ReadAll()

            $ps.Streams
    
            $ps.Stop()
            $ps.Dispose()
            $runspace.Close()
            $runspace.Dispose()
    
            if ($psResult.Count -gt 0) {
                $result = $psResult[0]
                $result
            }

            [gc]::Collect()

            sleep -Seconds 3

            Write-Verbose 'Exited END'
        }

        Write-Verbose 'Exited BEGIN'
    }

    process {
        # Write-Verbose 'Entered PROCESS'

        if (-not $form['Window'].IsVisible -or -not $form['LiveGrid']) {
            & $cleanup $true
            continue
        }
        else {
            $null = $form['GridData'].Add($_)
        }

        # Write-Verbose 'Exited PROCESS'
    }
 
    end {
        & $cleanup $false
    }
}

# gps | select -first 5 |
gci C: -File -Recurse |
Show-AnyBoxAsync -Message 'Hey there' -Topmost -ResizeMode 'CanResizeWithGrip' -WindowState 'Maximized' -Verbose