SendPingAsync Hanging when included in e

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Genx13
Posts: 12
Last visit: Wed Jun 16, 2021 5:29 am

SendPingAsync Hanging when included in e

Post by Genx13 »

I've got this code to check if a system is online, it works fine in Powershell ISE but when I compile it into an exe the exe will hang and not continue. Im not sure why, any help would be appreciated. I don't get any error or anything.

Code: Select all

[int]$Counter = '0'
		do
		{
			$PingTask = (New-Object System.Net.NetworkInformation.Ping).SendPingAsync($ComputerName)
			[Threading.Tasks.Task]::WaitAll()
			$Counter++
			If ($Counter -eq '3') { break }
			Start-Sleep -s 5
		}
		until (($PingTask).Result.Status -eq "Success")
If (($PingTask).Result.Status -eq "Success") {Write-Output 'Success'}
else {Write-Output 'Fail'}
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: SendPingAsync Hanging when included in e

Post by Alexander Riedel »

Sounds like a network timeout. Check if the ISE has a firewall exceptions and your packaged exe has not. Generally pings are blocked by the Windows firewall.
Also check if the ISE is running elevated versus your exe might not be doing that. You can play with the packaging options using elevation and STA mode as well to see if that makes a difference.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Genx13
Posts: 12
Last visit: Wed Jun 16, 2021 5:29 am

Re: SendPingAsync Hanging when included in exe

Post by Genx13 »

Pings are open on both systems firewalls. I can get results from both Admin and Non Admin ISE. I'm not familiar with STA mode?
Also the loop is set to break after 3 failed attempts so I would expect it to continue after a few minutes at most.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SendPingAsync Hanging when included in e

Post by jvierra »

"WaitAll" will not work correctly here under many circumstances.

Why use Async when you are just going to block with a wait?

If the thread throws an exception the code will never complete. You need to test the Status' for completion or failure.

The class is very tricky and is not designed to be used in this way. The class handles its own wait and timeout. "Ping" also has its own "Wait" that should be used and not the generic all tasks wait which can hang an EXE.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SendPingAsync Hanging when included in e

Post by jvierra »

Run the following and consider the results.

Code: Select all

$Counter = 3
$pinger = New-Object System.Net.NetworkInformation.Ping
for($i = 0;$i -le $Counter;$i++){
    $p = $pinger.SendPingAsync($ComputerName, 3000)
    Try{
        $p.Wait()
    }
    Catch{
        Write-Host "$_"
    }
    Finally{
        $p.Status
    }
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SendPingAsync Hanging when included in e

Post by jvierra »

Here is a more complete example showing how to run the pings and how to detect exceptions and completion.

Code: Select all

$computerName = 'nocomputer' 
$counter = 3
$timeoutMS = 500
$pingdelayMS = 2500
$pinger = New-Object System.Net.NetworkInformation.Ping

for($i = 0;$i -lt $counter;$i++){
    $p = $pinger.SendPingAsync($computerName, $timeoutMS)
    Try{
        $p.Wait()
    }
    Catch{
        Write-Host "$_" -ForegroundColor Red
    }
    Finally{
        Write-Host $p.Status -ForegroundColor Green
        if($p.Status -eq 'RanToCompletion'){
            Write-Host $p.Result.Status -Fore Blue -back White
        }
    }
    Start-Sleep -Milliseconds $pingdelayMS
}
The class is really designed to work with events and callbacks. It would normally be run on a separate thread that is managed by the main thread. This allows us to release many pings all at once and then NOT wait while the thread receives callbacks that deliver the status of the separate pings.

For normal PS use you should use Test-Connection or Test-NetConnection.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked