0

Everything I read online about this only shows how to send information to the UI which works for me now due to the comment suggestion. I am unable to find a way to do the reverse and send to an output while running a thread. The end goal is to use a WPF multithread with being able to interact with selenium running the background. grabbing and sending information to websites while still actively being able to use the WPF. I can do this in C# but the current job wants this all to be in PowerShell and this seems to be on the right track to do this. If I am wrong please guide me to the correct way to do something like this then.

Code:

Add-Type -AssemblyName PresentationFramework,PresentationCore,WindowsBase

Function Create-WPFWindow {
    Param($Hash)
    # Create a window object
    $Window = New-Object System.Windows.Window
    $Window.Width = '600'
    $Window.Height = '300'
    $Window.Title = 'WPF-CONTROL'
    $window.WindowStartupLocation = [System.Windows.WindowStartupLocation]::CenterScreen
    $Window.ResizeMode = [System.Windows.ResizeMode]::NoResize

# Create a Label object
$Label = New-Object System.Windows.Controls.Label
$Label.Height = 40
$Label.HorizontalContentAlignment = 'Left'
$Label.VerticalContentAlignment = 'Center'
$Label.FontSize = 15
$Label.Content = 'Activate:'
$Hash.Label = $Label

# Create a TextBlock object
$TextBlock = New-Object System.Windows.Controls.TextBlock
$TextBlock.Height = 150
$TextBlock.FontSize = 20
$TextBlock.TextWrapping = 'Wrap'
$TextBlock.Text = "Test"
$Hash.TextBlock = $TextBlock

# Create a Button1 object
$Button1 = New-Object System.Windows.Controls.Button
$Button1.Width = 300
$Button1.Height = 35
$Button1.HorizontalContentAlignment = 'Center'
$Button1.VerticalContentAlignment = 'Center'
$Button1.FontSize = 20
$Button1.Content = 'Start'
$Hash.Button1 = $Button1

# Assemble the window
$StackPanel1 = New-Object System.Windows.Controls.StackPanel
$StackPanel1.Margin = '150,20,5,5'
$StackPanel1.Orientation = 'Horizontal'
$StackPanel1.Children.Add($Button1)
$StackPanel2 = New-Object System.Windows.Controls.StackPanel
$StackPanel2.Margin = '5,5,5,5'
$StackPanel2.Orientation = 'Vertical'
$StackPanel2.Children.Add($Label)
$StackPanel2.Children.Add($TextBlock)
$StackPanel = New-Object System.Windows.Controls.StackPanel
$StackPanel.Margin = '5,5,5,5'
$StackPanel.Children.Add($StackPanel1)
$StackPanel.Children.Add($StackPanel2)
$Window.Content =  $StackPanel

# Stop the service and release the resources
$Window.Add_Closing({
    $Hash.On = $false
    $global:p.BeginInvoke()
    $global:p.Dispose()})
$Hash.Window = $Window
}

$Hash = [hashtable]::Synchronized(@{})
# Create a WPF window and add it to a Hash table
Create-WPFWindow $Hash | Out-Null

$Hash.Button1.Add_Click({
    if ($Hash.On -eq $true){
        $p.Stop()
        $Hash.On = $false
        $Hash.Button1.Background = 'Green'
        $Hash.Button1.Content = 'Start'
    }else{ 
        $Hash.On = $true
        $Hash.Button1.Background = 'Red'
        $Hash.Button1.Content = 'Stop'
    }
    $p.BeginInvoke() | Out-Null
})

$rs_dForm = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs_dForm.ApartmentState = 'STA'
$rs_dForm.ThreadOptions = 'ReuseThread'
$rs_dForm.Open()
$rs_dForm.SessionStateProxy.SetVariable('Hash', $Hash)

$p = [PowerShell]::Create().AddScript({
        Function global:FileEventListener (){
            if ($Hash.On){
                for ($i = 0; $i -lt 10; $i++) {
                    $Hash.Window.Dispatcher.Invoke([action]{$Hash.TextBlock.Text = "Test: " + $i.ToString()})
                #$Hash.TextBlock.Text = "Test: " + $i.ToString() #<----not updating?
                    Write-Output "Test: " + $i.ToString() #<----not possible?
                    Start-Sleep -Seconds 1
                }
            }
        }
        FileEventListener
})

$p.Runspace = $rs_dForm
# Show the window
$Hash.Window.ShowDialog() | Out-Null
user2455808
  • 95
  • 1
  • 1
  • 9
  • Does this answer your question? [Write PowerShell Output (as it happens) to WPF UI Control](https://stackoverflow.com/questions/23142137/write-powershell-output-as-it-happens-to-wpf-ui-control) – iRon Feb 17 '21 at 09:04
  • partly, this fixes me getting info to the UI but not the other way around. – user2455808 Feb 17 '21 at 13:46

0 Answers0