Guest User

Untitled

a guest
Mar 22nd, 2022
2,773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Example, GUI app that freezes
  2. [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
  3. $Form = [System.Windows.Forms.Form] @{
  4.     Text = 'My GUI Window'
  5. }
  6. $ListBox = [System.Windows.Forms.ListBox] @{
  7.     Dock = [System.Windows.Forms.DockStyle]::Fill
  8. }
  9. $Button = [System.Windows.Forms.Button] @{
  10.     Text = 'Do something'
  11.     Dock = [System.Windows.Forms.DockStyle]::Bottom
  12. }
  13. $Button.Add_Click(
  14.     {
  15.         $ListBox.Items.Insert(0, ('{0}: Do something' -f [Datetime]::Now))
  16.         [System.Threading.Thread]::Sleep(5000)
  17.         $ListBox.Items.Insert(0, ('{0}: Do something again' -f [Datetime]::Now))
  18.     }
  19. )
  20. [void] $Form.Controls.AddRange(@($ListBox, $Button))
  21. [void] $Form.ShowDialog()
  22.  
  23. # Example how to resolve the freeze issue with the same app
  24. $Sync = [HashTable]::Synchronized(@{})
  25. $Sync.Actions = [System.Collections.Queue]::new()
  26. $PowerShell = [PowerShell]::Create()
  27. $PowerShell.Runspace = [RunspaceFactory]::CreateRunspace()
  28. $PowerShell.Runspace.Open()
  29. $PowerShell.Runspace.SessionStateProxy.SetVariable('Sync', $Sync)
  30. # Run the GUI in a separate thread, that we dont freeze on user actions
  31. $Handle = $PowerShell.AddScript(
  32.     {
  33.         [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
  34.         $Sync.Form = [System.Windows.Forms.Form] @{
  35.             Text = 'My GUI Window'
  36.         }
  37.         $Sync.ListBox = [System.Windows.Forms.ListBox] @{
  38.             Dock = [System.Windows.Forms.DockStyle]::Fill
  39.         }
  40.         $Button = [System.Windows.Forms.Button] @{
  41.             Text = 'Do something'
  42.             Dock = [System.Windows.Forms.DockStyle]::Bottom
  43.         }
  44.         $Button.Add_Click(
  45.             {
  46.                 $Sync.Actions.Enqueue('Do something')
  47.             }
  48.         )
  49.         [void] $Sync.Form.Controls.AddRange(@($Sync.ListBox, $Button))
  50.         [void] $Sync.Form.ShowDialog()
  51.     }
  52. ).BeginInvoke()
  53.  
  54. # While the handle of the separate thread we created is still running
  55. While (!$Handle.IsCompleted) {
  56.     While ($Sync.Actions.Count -gt 0) {
  57.         # Manage the actions we queue from inside
  58.         Switch ($Sync.Actions.Dequeue()) {
  59.             'Do something' {
  60.                 $Sync.ListBox.Items.Insert(0, ('{0}: Do something' -f [Datetime]::Now))
  61.                 [System.Threading.Thread]::Sleep(5000)
  62.                 $Sync.ListBox.Items.Insert(0, ('{0}: Do something again' -f [Datetime]::Now))
  63.             }
  64.         }
  65.     }
  66.     [System.Threading.Thread]::Sleep(100)
  67. }
Advertisement
Add Comment
Please, Sign In to add comment