Refresh $WPA.label multiple times with one click?

Hi,
so i’ve been trying to create my first GUI. For this question i’ve made a testcase. I hava an inputfield, if this -ieq “k” then first print “ok” then change it to “lets go”. At the moment, it only shows Lets go.

$WPFbutton.Add_Click({
    if($WPFtextBox.Text -ieq "k"){
     $WPFlabel.Content = "ok"
      Write-host "Updating Window"
      sleep 5        
     $WPFlabel.Content = "lets go"
     Write-host "Updating Window 2"
    }
})

Does anyone had an example? Or a suggestion? I"ve been googeling but can’t find it.

Does this do the trick?

$WPFbutton.Add_Click({
    if($WPFtextBox.Text -ieq "k"){
     $WPFlabel.Content = "ok"
     $WPFlabel.Refresh()
      Write-host "Updating Window"
      sleep 5        
     $WPFlabel.Content = "lets go"
     $WPFlabel.Refresh()
     Write-host "Updating Window 2"
    }
})

Hi Steven,

No sadly not. I tried that before.
I keep getting the following error:

Updating Window
Method invocation failed because [System.Windows.Controls.Label] does not contain a method named 'Refresh'.
At C:\Users\9081059\AppData\Local\Temp\dc3c8e6c-4353-4d63-8fff-2d6e22b930bc.ps1:88 char:6
+      $WPFlabel.Refresh()
+      ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Can you send me your full code? If not, try using .Refresh() against the form itself.

Sure, no problem

$inputXML = @"

    
        
            
                
                
            
        

        
            
                
            
        
        
        
        

        
            
                
                    
                    
                    
                    
                
            
        
        
        
            
                
            
        
        
        
        
        
    
 
"@       
 
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'
 
 
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
 
    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
  try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
 
#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================
 
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
 
Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}
 
Get-FormVariables

#===========================================================================
# Actually make the objects work
#===========================================================================

$WPFbutton.Add_Click({
	$WPFlistView.Items.Clear()	
	$personeelsnummer = $WPFtextBox.Text
	
	if($WPFtextBox.Text -ieq "k"){
     $WPFlabel1.Content = "ok"
     $Form.Refresh()
      Write-host "Updating Window"
     
     $WPFlabel1.Content = "lets go"
     $Form.Refresh()
     Write-host "Updating Window 2"
    }
})

#debug
$Form | gm | Select Name, Membertype | Out-GridView

$Form.ShowDialog() | out-null
$Form.Refresh()  results in : 
Method invocation failed because [System.Windows.Window] does not contain a met
hod named 'Refresh'.
At line:90 char:6
+      $Form.Refresh()
+      ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Does seem the have problem posting the code here.
So i pasted it in pastebin : http://pastebin.com/Npbi1U6h

with WPF, you may want to try using this:

https://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx

Care to share your code :slight_smile: ? cause no mather where i place InvalidateVisual()
$form.InvalidateVisual() or on the label, the results stays the same :’(

Anyone else has an idea?

Have you nanaged to get a WPF label to refresh in C#? it should be a matter of copying the method from there really. Very busy day in the office but I’m curious and will investigate later if I have time

You are likely running into a common issue with PowerShell and GUIs. Since PowerShell will by default process your code as a single thread. In a lot of situations, the thread gets to processing something else, so the GUI cannot update. Check out this thread on a similar topic with a progress bar. There is also a link there to an innovative solution so that the GUI runs in a separate processing thread.

https://powershell.org/forums/topic/progressbar-example-starting-at-wrong-value/#post-31849

I don’t think you will need to start using multiple threads, as in theory you are only running a method to refresh. If you were running this at the same time as trying to run a command which would take 5 seconds to invoke, I’d probably agree.

Powershell can use whatever assemblies you import, and doesn’t lack any of the ability of other languages to actually execute, only how many process it can run simultaneously.

could you please upload your code to pastebin again? I have some free time this afternoon.

Also, in my WinForms, I dot this in certain places, which did fix some issues powershell had with carrying out events:

[System.Windows.Forms.Application]::DoEvents() 

WPF may require something else. As I said, if you can upload again, I will have a look.

I will be interested to see what Steven finds, but I can say for sure that I have seen this issue particularly with the start-sleep cmdlet since start-sleep keeps the thread from processing anything while it’s sleeping. I will also say that I don’t usually see this issue with labels. It’s usually more complex form controls that I have seen this on, like progress bars.

I have created a new GUI just for testing purposes.
It only has one button and a label that has to change twices.
If we get this working, i can build my full GUI again.

My application that i needed to make is done by creating a GUI typing out every item of the layout (HTML style).
But i hope we get this working so i can use Visual Studio to create my interfaces, it will save a lot of time.

Anyway , the pastebin : http://pastebin.com/TjWEigZ8

And thanks again for having a look

$WPFbutton.Add_Click({

	$WPFlabel1.Content = " "    
    $WPFlabel1."Getting data from AD..."
    $WPFlabel1.InvalidateVisual()
    Write-host "Updating Window"
    sleep 3
    $WPFlabel1.Content = "Succes, show results, lets go!"
    $WPFlabel1.InvalidateVisual()
    Write-host "Updating Window 2"
    
})

That works for me :slight_smile:

Really?

I tried that before, it didn’t work :confused:
I tried it again now, still doesn’t work.

Name Value


PSVersion 5.0.10514.6
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 10.0.10514.6
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3

It’s only updating once so far, which is once the method has executed.

There are ways you can allow the label to update mid method execution, but you’d need to try to translate from C#
https://jasonalls.wordpress.com/2013/02/14/update-wpf-ui-during-method-execution/

To be honest, If I were you, I’d use WinForms. it’s tried and tested far more than WPF, and VERY easily used in PowerShell.

I’d try translating yourself, I want to give it a go myself too!

Yeah i created my first GUI now with WinForms. Just is a pitty that you can’t “draw” your layout like WPF. But it will have to do i suppose.

Its not that i’m unwilling but i’m new to Powershell, if i’ve got to learn C# too, it might be a bit too much. I want to focus my time on Powershell :slight_smile:

I was where you were 6 months ago. Once I learnt how to use Powershell syntax inside out (by doing massive complex projects in my spare time). I started on C# and it’s opened up a whole other side of Powershell, namely because you can interact with .NET better.

Good luck :slight_smile:

I’m actually looking at getting that method to run in powershell, so I’ll let you know if I get it working.

I might not be thát talented :slight_smile:
What projects did you work on ?

these are things that i worked on http://pastebin.com/cJRuXnKx