Stopping UDDynamic and UDChartJSMonitor gracefully?

Is there a good way to stop a UDDynamic or UDChartJSMonitor? (Or I guess any other auto-refreshing component?

An example of what I’m working on is below… Enter in a Computer Name you can ping and click “Monitoring” and enter in the minutes to ping the device…

Essentially, if I am pinging a computer for 1 minute, I want the UDDynamic to run 60~ times (probably closer to 58-59 depending on how long it takes to run the scriptblock) and then stop. I then want the monitor to preserve the current state, but stop refreshing.

$Pages = @()
$Pages += New-UDPage -Name 'Home' -Url '/Test/' -Content {
    New-UDForm -Children {
        New-UDTextbox -id 'txtComputerName' -Label "ComputerName"
    } -OnSubmit {
        Invoke-UDRedirect -Url "/Test/$($EventData.txtComputerName)"
    }
}

$Pages += New-UDPage -Name 'Ping Monitor' -Url '/Test/:Computername' -Content {
    If (-not $Session:Data) {
        $Session:Data = [Hashtable]::Synchronized(@{})
    }
    Else {
        Foreach ($Key in ($Session:Data.keys | Where-Object { $_ -like "*$Computername" }) ) {
            $Session:Data.Remove($Key)
        }
    }
    New-UDDynamic -Content {
        If (-not $PingObj) {
            $PingObj = New-Object System.Net.NetworkInformation.Ping
        }
        $Session:Data.Add("Ping$ComputerName", ($PingObj.Send("$ComputerName", 1000) | select Address, RoundTripTime, Status))
        If (($Session:Data["Ping$ComputerName"].Status).ToString() -eq "Success") {
            New-UDGrid -Item -Content {
                New-UDList -SubHeader "Network Info" -Content {
                    New-UDButton -Text "Monitoring" -OnClick {
                        Show-UDModal -FullScreen -Content {
                            If ($Session:Data["PingMonitor$ComputerName"]) {
                                $session:Data.Remove("PingMonitor$ComputerName")
                            }
                            $Session:Data.Add("PingMonitor$ComputerName", (New-Object System.Collections.Generic.List[Object]))
                            New-UDForm -Children {
                                New-UDTextbox -Id 'txtPingMon' -Label 'Minutes to Monitor' -Mask '0[0]'
                            } -OnValidate {
                                If ($EventData.txtPingMon -eq '') {
                                    New-UDFormValidationResult -ValidationError "Enter in Minutes to monitor connection."
                                }
                                else {
                                    New-UDFormValidationResult -Valid
                                }
                            } -OnSubmit {
                                $End = (Get-Date).AddMinutes($EventData.txtPingMon)
                                New-UDDynamic -id "dynPingMonitor$ComputerName" -Content {
                                    If ((Get-Date) -le $ArgumentList[0]) {
                                        $Ping = $PingObj.Send("$ComputerName", 1000) | select Address, RoundTripTime, Status, @{Name = 'Computer' ; Expression = { $ComputerName } }, @{Name = 'TimeStamp' ; Expression = { Get-Date } }
                                        $Session:Data["PingMonitor$ComputerName"].Add($Ping)
                                        Sync-UDElement -Id 'tblPingMonitor'
                                    }
                                    else {
                                        Set-UDElement -Id 'crtPingMonitor' -Properties @{ AutoRefresh = $false }
                                        Remove-UDElement -id "dynPingMonitor$ComputerName"
                                    }
                                } -ArgumentList $End -AutoRefresh -AutoRefreshInterval 1
                                New-UDChartJSMonitor -Type line -DataPointHistory 60 -id 'crtPingMonitor' -LoadData {
                                    ($Session:Data["PingMonitor$ComputerName"] | Sort-Object TimeStamp -Descending | select -First 1 -ExpandProperty RoundTripTime) | Out-UDChartJSMonitorData
                                } -Labels "Ping" -ChartBackgroundColor "#297741" -AutoRefresh -RefreshInterval 1
                            }
                            $PingColumns = @(
                                New-UDTableColumn -Property Address -Title 'IP'
                                New-UDTableColumn -Property RoundTripTime -Title 'Ping'
                                New-UDTableColumn -Property Status -Title 'Status'
                                New-UDTableColumn -Property 'Computer'
                                New-UDTableColumn -Property Timestamp -Render {
                                    New-UDDateTime -InputObject $EventData.Timestamp -Format 'HH:mm:ss'
                                }
                            )
                            New-UDTable -id 'tblPingMonitor' -Title "Ping Monitor" -columns $PingColumns -LoadData {
                                $PingMon = $Session:Data["PingMonitor$ComputerName"]
                                $PingMon | Sort-Object TimeStamp -Descending | Out-UDTableData -TotalCount $PingMon.count -Properties $Eventdata.Properties -Page $EventData.Page
                            }
                        } -Persistent -Footer {
                            New-UDButton -text "Close" -OnClick {
                                Hide-UDModal
                            }
                        }
                    }
                    New-UDListItem -Label 'IP' -Icon (New-UDIcon -Icon Heartbeat -color Green -Size 1x) -SubTitle "$(($Session:Data["Ping$ComputerName"] ).Address.IPAddressToString)"
                    New-UDListItem -Label 'Ping' -Icon (New-UDIcon -Icon Clock -color Green -Size 1x) -SubTitle "$(($Session:Data["Ping$ComputerName"] ).RoundTripTime)ms"
                }
            }
        }
        ElseIf (($Session:Data["Ping$ComputerName"].Status).ToString() -eq "TimedOut") {
            New-UDGrid -Item -Content {
                New-UDList -SubHeader "Network Info" -Content {
                    New-UDListItem -Label 'IP' -Icon (New-UDIcon -Icon Heartbeat -color Red -Size 1x) -SubTitle "$(($Session:Data["Ping$ComputerName"]).Address.IPAddressToString)"
                    New-UDListItem -Label 'Ping' -Icon (New-UDIcon -Icon Clock -color Red -Size 1x) -SubTitle "$(($Session:Data["Ping$ComputerName"]).RoundTripTime)ms"
                }
            }
        }
        Else {
            New-UDGrid -Item -Content {
                New-UDList -SubHeader "Network Info" -Content {
                    New-UDListItem -Label 'Host not found' -Icon (New-UDIcon -Icon HeartBroken -color Red -Size lg)
                }
            }
        }
    }    
}

New-UDDashboard -Title 'Pages' -Pages $Pages
Product: PowerShell Universal
Version: 3.2.8

Hello, you can stop the autorefresh like this :

Set-UDElement -Id $Id -Properties @{
    autoRefresh = $false
}
2 Likes