Advertisement
filimonic

Powershell Example Parallel Forms

Oct 3rd, 2021
1,879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
  2.  
  3. $Global:tableRun = [System.Collections.Generic.List[PSCustomObject]]::new()
  4.  
  5. function Get-CheckTableRun
  6. {
  7.     param(
  8.        $tableRun = $tableRun
  9.     )
  10.     $objectsToRemove = [System.Collections.Generic.List[PSCustomObject]]::new()
  11.     foreach ($item in $tableRun)
  12.     {
  13.         if ($item.handle.IsCompleted) {
  14.             $objectsToRemove.Add($item)
  15.             $item.Label.Text = 'Ok'
  16.             $result = $item.powershell.EndInvoke($item.handle)
  17.             $Global:Result = $result
  18.             $item.powershell.Stop()
  19.             $item.Button.Enabled = $true
  20.             foreach ($resultObject in $result) {
  21.                 Write-Host -f Yellow "$($resultObject.TaskGuid)`t$($resultObject.Value)"
  22.             }
  23.         }
  24.         $form.Refresh()
  25.     }
  26.  
  27.     foreach ($item in $objectsToRemove) {
  28.         $tableRun.Remove($item)
  29.     }
  30.     $objectsToRemove.Clear()
  31. }
  32.  
  33. function Get-RunClick
  34. {
  35.     param(
  36.         $Label,
  37.         $Button,
  38.         $NameRunJob = 'Task1'
  39.     )
  40.    
  41.     $hash = [hashtable]::Synchronized(@{})
  42.     $hash.value = 1
  43.  
  44.     $runspace = [runspacefactory]::CreateRunspace()
  45.     $runspace.Open()
  46.     $runspace.SessionStateProxy.SetVariable('Hash',$hash)
  47.     $runspace.SessionStateProxy.SetVariable('Label',$Label)
  48.     $powershell = [powershell]::Create()
  49.     $powershell.Runspace = $runspace
  50.    
  51.     $powershell.AddScript(
  52.     {
  53.         $guid = [Guid]::NewGuid()
  54.         for ($i = 1; $i -lt 100; $i++)
  55.         {
  56.             $hash.value = $i
  57.             Start-Sleep -Milliseconds 50
  58.             $Label.Text = $i
  59.             Write-Output( [PSCustomObject]@{TaskGuid=$guid; Value=$i})
  60.         }
  61.     }) | Out-Null
  62.  
  63.     $handle = $powershell.BeginInvoke()
  64.     $Button.Enabled = $false
  65.     $Global:tableRun.Add([pscustomobject]@{
  66.         handle = $handle
  67.         JobName = "Task1"
  68.         Label = $Label
  69.         Powershell = $powershell
  70.         Button = $Button
  71.         #JobScriptPath = $jibFilePath
  72.     })
  73.  
  74. }
  75.  
  76.  
  77. $form = New-Object Windows.Forms.Form
  78.  
  79. $timer = New-Object System.Windows.Forms.Timer
  80. $timer.Interval = 10
  81.  
  82. $button1 = New-Object Windows.Forms.Button
  83. $button1.text = "button1"
  84. $button1.Location = New-Object System.Drawing.Size(10,5)
  85.  
  86. $button1.add_click({Get-RunClick -Label $Label1 -Button $button1})
  87.  
  88. $form.controls.add($button1)
  89.  
  90. $Label1 = New-Object Windows.Forms.Label
  91. $Label1.text = "Status"
  92. $Label1.Location = New-Object System.Drawing.Size(100,8)
  93. $Form.Controls.Add($Label1)
  94.  
  95.  
  96. $button2 = New-Object Windows.Forms.Button
  97. $button2.text = "button2"
  98. $button2.Location = New-Object System.Drawing.Size(10,30)
  99.  
  100. $button2.add_click({Get-RunClick  -Label $Label2 -Button $button2})
  101.  
  102. $form.controls.add($button2)
  103.  
  104. $Label2 = New-Object Windows.Forms.Label
  105. $Label2.text = "Status"
  106. $Label2.Location = New-Object System.Drawing.Size(100,38)
  107. $Form.Controls.Add($Label2)
  108.  
  109.  
  110. $timer.add_Tick({Get-CheckTableRun $tableRun})
  111. $timer.Enabled = $true
  112.  
  113. $form.ShowDialog()
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement