Trying to update a textbox with a start-job and a register-objectevent

64 Views Asked by At

i made this script that create a timer and get the infomration inside a job

function get-pingtest { 
    $script:timer1 = New-Object System.Timers.Timer 
    $timer1.Interval = 200 
    $timer1.AutoReset = $true 
    $timer1.Start()
    $PingArgs = @{
        TargetName = "google.com"
        count = 2
        BufferSize = 64
        TimeoutSeconds = 1
    }
    $TimerArgs = @{
        InputObject = $Timer1
        EventName = 'Elapsed'
        SourceIdentifier = 'Timer.Ping'
        Action = { 
            if ((get-job -Name Ping).state -eq "Stopped"){
                $JobOutput = receive-job -Name Ping
                Write-host ($JobOutput)
                if ($null -eq $JobOutput) {
                    $timer1.Stop()
                    Unregister-Event -SourceIdentifier *
                    remove-job -name * -force
                }
            }
            if ((get-job -name Ping).state -eq "Failed"){
                $JobOutput = receive-job -Name Ping 2>&1
                Write-Host "$($JobOutput | Out-String)"
                $timer1.Stop()
                Unregister-Event -SourceIdentifier *
                remove-job -name * -force
            }
            if ((get-job -name Ping).state -eq "Completed"){
                $JobOutput = receive-job -Name Ping 2>&1
                if ($null -eq $JobOutput) {
                    $timer1.Stop()
                    Unregister-Event -SourceIdentifier *
                    remove-job -name * -force
                }
                else {
                    Write-Host "$($JobOutput | Out-String)"
                }
            }
            if ((get-job -name Ping).state -eq "Running"){
                $JobOutput = receive-job -Name Ping
                if ($null -eq $JobOutput) {}
                else {Write-Host "$($JobOutput | Out-String)"}
            }
        }
    }
    Start-Job -name Ping -ScriptBlock {Test-Connection @using:PingArgs}
    Register-ObjectEvent @TimerArgs
}

and works as intended it retrive the information without any problem even if there is some error in the job process

but when i insert it in my program sometings goes wrong the script in my program is this

function start-advancedpingtimed  {
    $script:timer1 = New-Object System.Timers.Timer 
    $timer1.Interval = 200 
    $timer1.AutoReset = $true 
    $timer1.Start()
$PingArgs = @{
    TargetName = "google.com"
    count = 2
    BufferSize = 64
    TimeoutSeconds = 1
}
$TimerArgs = @{
    InputObject = $Timer1
    EventName = 'Elapsed'
    SourceIdentifier = 'Timer.Ping'
    Action = {  
        if ((get-job -Name Ping).state -eq "Stopped"){
            $JobOutput = receive-job -Name Ping
            if ($null -eq $JobOutput) {
                $timer1.Stop()
                Unregister-Event -SourceIdentifier *
                remove-job -name * -force
            }
            else {Write-toOutputBox "$($JobOutput | Out-String)"}
        }
        if ((get-job -name Ping).state -eq "Failed"){
            $JobOutput = receive-job -Name Ping 2>&1
            Write-toOutputBox -iserror "$($JobOutput | Out-String)"
            $timer1.Stop()
            Unregister-Event -SourceIdentifier *
            remove-job -name * -force
        }
        if ((get-job -name Ping).state -eq "Completed"){
            $JobOutput = receive-job -Name Ping 2>&1
            if ($null -eq $JobOutput) {
                $timer1.Stop()
                Unregister-Event -SourceIdentifier *
                remove-job -name * -force
            }
            else {Write-toOutputBox "$($JobOutput | Out-String)"}
        }
        if ((get-job -name Ping).state -eq "Running"){
            $JobOutput = receive-job -Name Ping
            if ($null -eq $JobOutput) {}
            else {Write-toOutputBox "$($JobOutput | Out-String)"}
        }
    }
}
Start-Job -name Ping -ScriptBlock {Test-Connection @using:PingArgs}
Register-ObjectEvent @TimerArgs
}

and this is the script to update the text box

function Write-toOutputBox {
    [CmdletBinding()]
    param (
        [array]$Message,
        [switch]$clear,
        [switch]$iserror
    )
    if($clear){$wpf.TextBox_Output.Clear()}
    if ($iserror) {$wpf.TextBox_Output.Foreground = "Red"}
    else {$wpf.TextBox_Output.Foreground = "Black"}
    foreach($m in $Message) {
        if ($null -ne $m) {
            $wpf.TextBox_Output.text += "$m `r"
            $wpf.TextBox_Output.ScrollToEnd()
        }
        else{continue}
    }
}

so when start the function from the gui, either powershel inside vs code crashes, or dont update anyting end by closing the gui, watching inside the textbox variable i see the results

0

There are 0 best solutions below