Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
- $Global:tableRun = [System.Collections.Generic.List[PSCustomObject]]::new()
- function Get-CheckTableRun
- {
- param(
- $tableRun = $tableRun
- )
- $objectsToRemove = [System.Collections.Generic.List[PSCustomObject]]::new()
- foreach ($item in $tableRun)
- {
- if ($item.handle.IsCompleted) {
- $objectsToRemove.Add($item)
- $item.Label.Text = 'Ok'
- $result = $item.powershell.EndInvoke($item.handle)
- $Global:Result = $result
- $item.powershell.Stop()
- $item.Button.Enabled = $true
- foreach ($resultObject in $result) {
- Write-Host -f Yellow "$($resultObject.TaskGuid)`t$($resultObject.Value)"
- }
- }
- $form.Refresh()
- }
- foreach ($item in $objectsToRemove) {
- $tableRun.Remove($item)
- }
- $objectsToRemove.Clear()
- }
- function Get-RunClick
- {
- param(
- $Label,
- $Button,
- $NameRunJob = 'Task1'
- )
- $hash = [hashtable]::Synchronized(@{})
- $hash.value = 1
- $runspace = [runspacefactory]::CreateRunspace()
- $runspace.Open()
- $runspace.SessionStateProxy.SetVariable('Hash',$hash)
- $runspace.SessionStateProxy.SetVariable('Label',$Label)
- $powershell = [powershell]::Create()
- $powershell.Runspace = $runspace
- $powershell.AddScript(
- {
- $guid = [Guid]::NewGuid()
- for ($i = 1; $i -lt 100; $i++)
- {
- $hash.value = $i
- Start-Sleep -Milliseconds 50
- $Label.Text = $i
- Write-Output( [PSCustomObject]@{TaskGuid=$guid; Value=$i})
- }
- }) | Out-Null
- $handle = $powershell.BeginInvoke()
- $Button.Enabled = $false
- $Global:tableRun.Add([pscustomobject]@{
- handle = $handle
- JobName = "Task1"
- Label = $Label
- Powershell = $powershell
- Button = $Button
- #JobScriptPath = $jibFilePath
- })
- }
- $form = New-Object Windows.Forms.Form
- $timer = New-Object System.Windows.Forms.Timer
- $timer.Interval = 10
- $button1 = New-Object Windows.Forms.Button
- $button1.text = "button1"
- $button1.Location = New-Object System.Drawing.Size(10,5)
- $button1.add_click({Get-RunClick -Label $Label1 -Button $button1})
- $form.controls.add($button1)
- $Label1 = New-Object Windows.Forms.Label
- $Label1.text = "Status"
- $Label1.Location = New-Object System.Drawing.Size(100,8)
- $Form.Controls.Add($Label1)
- $button2 = New-Object Windows.Forms.Button
- $button2.text = "button2"
- $button2.Location = New-Object System.Drawing.Size(10,30)
- $button2.add_click({Get-RunClick -Label $Label2 -Button $button2})
- $form.controls.add($button2)
- $Label2 = New-Object Windows.Forms.Label
- $Label2.text = "Status"
- $Label2.Location = New-Object System.Drawing.Size(100,38)
- $Form.Controls.Add($Label2)
- $timer.add_Tick({Get-CheckTableRun $tableRun})
- $timer.Enabled = $true
- $form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement