Advertisement
Guest User

Doevents in WPF for Powershell

a guest
Feb 17th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName PresentationFramework
  2. [xml]$Form = @"
  3.    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.        Title="MainWindow" Height="150" Width="325">
  5.    <Grid>
  6.        <TextBlock Name="TextBlock" Text="0" HorizontalAlignment="Center"/>
  7.        <Button Name="button_Do" Height="35" Width="120" Content="Do"/>
  8.    </Grid>
  9. </Window>
  10. "@
  11.  
  12. Function DoEvents()
  13. {
  14.     $frame = New-Object System.Windows.Threading.DispatcherFrame
  15.     [System.Windows.Threading.DispatcherOperationCallback]$CallBack = { $frame.Continue = $false }
  16.     $CurrentDispatcher = [System.Windows.Threading.Dispatcher]::CurrentDispatcher
  17.     $CurrentDispatcher.BeginInvoke("Background", $CallBack, $null)
  18.     [System.Windows.Threading.Dispatcher]::PushFrame($frame)
  19. }
  20.  
  21. $Reader = (New-Object System.Xml.XmlNodeReader $Form)
  22. $Window = [Windows.Markup.XamlReader]::Load($Reader)
  23.  
  24. $TextBlock = $Window.FindName("TextBlock")
  25. $button_Do = $Window.FindName("button_Do")
  26. $button_Do.Add_Click({
  27.     for ($i=1; $i -le 5; $i++)
  28.     {
  29.         $window.Title = $i
  30.         $TextBlock.text = $i
  31.         DoEvents
  32.         #[System.Windows.Threading.Dispatcher]::CurrentDispatcher.Invoke("Background", [action]{ })
  33.         #$Window.Dispatcher.Invoke("Background", [action]{})
  34.         Start-sleep 1
  35.     }
  36. })
  37.  
  38. $Window.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement