Following on from my last post, I’m going to show you how to update a textbox using a button on the same form. I will be adding the following code starting on line 38:
#BUTTON LOGIC $syncHash.Button.Add_Click({ $syncHash.Window.Dispatcher.Invoke( [action]{ $syncHash.TextBox.AppendText("This is a test") } ) })
This is fairly basic in what it does. It just adds “This is a test” to the textbox. Say if I want the button to run a task and then update the textbox with the results, but the results took a long time to come, the form would freeze. This is because whatever command you run in the same runspace as the GUI, takes controls and stops the GUI being responsive.
So, what I’m going to do is ping google 5 times, get the average from all of those and then update the textbox without the GUI becoming unresponsive. To do this, I’m going to create a new runspace and add the code I want to run. You can see this below:
#BUTTON LOGIC $syncHash.Button.Add_Click({ #ASSIGNING HOST VARIABLE $syncHash.host = $Host #CREATING NEW RUNSPACE $pingrunspace = [runspacefactory]::CreateRunspace() $pingrunspace.ApartmentState = "STA" $pingrunspace.ThreadOptions = "ReuseThread" $pingrunspace.Open() #PUTTING THE SYNCHASH VARIABLE INSIDE THE NEW RUNSPACE $pingrunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) #THIS IS THE CODE THAT WILL BE EXECUTED IN THE NEW RUNSPACE $code = { #CONNECTION TO GOOGLE AND CALCULATING AVERAGE IN NEW RUNSPACE $connection = Test-Connection -ComputerName google.co.uk -Count 5 $average = [math]::Round(($connection.responsetime | Measure-Object -Average).Average) #UPDATING THE TEXTBOX WITH CONNECTION AVERAGE IN NEW RUNSPACE $syncHash.Window.Dispatcher.Invoke( [action]{ $syncHash.TextBox.AppendText($average) } ) } #ADDING AND RUNNING THE CODE IN THE NEW RUNSPACE $PSInstance = ::Create().AddScript($code) $PSinstance.Runspace = $pingrunspace $job = $PSinstance.BeginInvoke() })
This will run the code in a separate runspace to the GUI and allow you to interact with it whilst the commands complete in the background.
Just in case you want the entire this, this is what the whole file looks like 🙂
#CREATE HASHTABLE AND RUNSPACE FOR GUI $syncHash = [hashtable]::Synchronized(@{}) $newRunspace =[runspacefactory]::CreateRunspace() $newRunspace.ApartmentState = "STA" $newRunspace.ThreadOptions = "ReuseThread" $newRunspace.Open() $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) #BUILD GUI AND ADD TO RUNSPACE CODE $psCmd = [PowerShell]::Create().AddScript({ $xaml = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Name="Window" Height="400" Width="600"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Button Name="Button" Content="Press" Height="200" Width="580" Grid.Row="0" Grid.Column="0" /> <TextBox Name="Textbox" Height="200" Width="580" Grid.Row="1" Grid.Column="0" /> </Grid> </Window> "@ #INTERPRET AND LOAD THE GUI $reader=(New-Object System.Xml.XmlNodeReader $xaml) $syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader ) #EXTRACT THE CONTROLS FROM THE GUI $syncHash.TextBox = $syncHash.window.FindName("Textbox") $syncHash.Button = $syncHash.Window.FindName("Button") #BUTTON LOGIC $syncHash.Button.Add_Click({ $syncHash.host = $Host $pingrunspace = [runspacefactory]::CreateRunspace() $pingrunspace.ApartmentState = "STA" $pingrunspace.ThreadOptions = "ReuseThread" $pingrunspace.Open() $pingrunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) $code = { $connection = Test-Connection -ComputerName google.co.uk -Count 5 $average = [math]::Round(($connection.responsetime | Measure-Object -Average).Average) $syncHash.Window.Dispatcher.Invoke( [action]{ $syncHash.TextBox.AppendText($average) } ) } $PSInstance = ::Create().AddScript($code) $PSinstance.Runspace = $pingrunspace $job = $PSinstance.BeginInvoke() }) #FINALISE AND CLOSE GUI RUNSPACE UPON EXITING $syncHash.Window.ShowDialog() | Out-Null $syncHash.Error = $Error $Runspace.Close() $Runspace.Dispose() }) #LOAD RUNSPACE WITH GUI IN $psCmd.Runspace = $newRunspace $data = $psCmd.BeginInvoke()
Enjoy!