Thread RunspacePool. I need help!

Welcome Forums Pester Thread RunspacePool. I need help!

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #105775

    Participant
    Points: 0
    Rank: Member

    I'm writing a script to get the status of services. I have a strange error and I can not find a problem. I ran the script many times and each time I received a different result. The computer name does not exist, but the thread update service for this computer indicates that the service is running. Details show in picture below:

    Image

    function update status:

    
     
    
    function Button_Click_update_status {
    
    $Button_update_status.Enabled = $false
    
    #####################################################
    
    $Global:progress = 0
    $ProgressBar1.Value = $Global:progress
    
    $line_count = $DataGridView1.RowCount
    
    $Global:progress_max = $line_count
    [float]$progressf = 0
    
    #$RunspacePool = [RunspaceFactory ]::CreateRunspacePool(1, 8)
    
    $Jobs = @()
    
    #$RunspacePool.Open()
    
    For ($i=0; $i -le $line_count-1; $i++) {
    
    $Job = [powershell]::Create().AddScript($ScriptBlock_get_service_status)
    $Job.AddArgument($DataGridView1.Rows[$i].Cells[0].Value)
    $Job.AddArgument($Global:service_name)
    $Job.RunspacePool = $Global:RunspacePoolG
    
    $Jobs += New-Object PSObject -Property @{
    RunNum = $_
    Pipe = $Job
    rowIndex = $i
    row_updated = $false
    pcName = $DataGridView1.Rows[$i].Cells[0].Value
    Result = $Job.BeginInvoke()
    }
    log("BeginInvoke()")
    
    $Global:progress = [math]::Round($i * 100/$Global:progress_max*2/3)
    
    $ProgressBar1.Value = $Global:progress
    $ProgressBar1.refresh()
    
    }
    
    Do{
    if($ProgressBar1.Value -le 90){
    $progressf = $progressf +0.2
    $Global:progress = $progressf
    $ProgressBar1.Value = $Global:progress
    $ProgressBar1.refresh()
    }
    log($Global:progress)
    [string]$service = ""
    ForEach ($Job in $Jobs){
    if(($Job.Result.IsCompleted -eq $true) -and ($Job.row_updated -eq $false)){
    $service = $Job.Pipe.EndInvoke($Job.Result)
    $rowIndex = $Job.rowIndex
    log("rowIndex = "+ $rowIndex + " " +$service)
    log("Job.Result "+$Job.Result)
    log("pc_name = "+$Job.pcName)
    
    if(($service -eq "") -Or ($service -eq $null)){
    $DataGridView1.Rows[$rowIndex].Cells[4].Value = "not found"
    $DataGridView1.Rows[$rowIndex].Cells[4].Style.backcolor = "Pink"
    
    }else {
    $DataGridView1.Rows[$rowIndex].Cells[4].Value = $service
    if($DataGridView1.Rows[$rowIndex].Cells[4].Value -eq "Running"){
    $DataGridView1.Rows[$rowIndex].Cells[4].Style.backcolor = "White"
    } else{
    $DataGridView1.Rows[$rowIndex].Cells[4].Style.backcolor = "Yellow"
    }
    }
    $Job.row_updated = $true
    $Job.Pipe.Dispose()
    
    }
    }
    
    Start-Sleep -m 200
    } While ($Jobs.Result.IsCompleted -contains $false)
    
    [string]$service = ""
    ForEach ($Job in $Jobs){
    if(($Job.Result.IsCompleted -eq $true) -and ($Job.row_updated -eq $false)){
    $service = $Job.Pipe.EndInvoke($Job.Result)
    $rowIndex = $Job.rowIndex
    log("rowIndex = "+ $rowIndex + " " +$service)
    log("Result= "+$Job.Result)
    log("pc_name = "+$Job.pcName)
    
    if(($service -eq "") -Or ($service -eq $null)){
    $DataGridView1.Rows[$rowIndex].Cells[4].Value = "not found"
    $DataGridView1.Rows[$rowIndex].Cells[4].Style.backcolor = "Pink"
    
    }else {
    $DataGridView1.Rows[$rowIndex].Cells[4].Value = $service
    if($DataGridView1.Rows[$rowIndex].Cells[4].Value -eq "Running"){
    $DataGridView1.Rows[$rowIndex].Cells[4].Style.backcolor = "White"
    } else{
    $DataGridView1.Rows[$rowIndex].Cells[4].Style.backcolor = "Yellow"
    }
    }
    $Job.row_updated = $true
    $Job.Pipe.Dispose()
    
    }
    }
    
    $Jobs.Clear()
    #$RunspacePool.Close()
    #$RunspacePool.Dispose()
    
    log("RunspacePool.Close()")
    log($service)
    
    $ProgressBar1.Value = 100
    
    $Button_update_status.Enabled = $true
    }
    
     
  • #105776

    Participant
    Points: 0
    Rank: Member
  • #108679

    Participant
    Points: 587
    Helping Hand
    Rank: Major Contributor

    You have to use gist (via github) or the 'pre' tags for posting script in the forum.

The topic ‘Thread RunspacePool. I need help!’ is closed to new replies.