Page 1 of 1

SendPingAsync Hanging when included in e

Posted: Wed Oct 31, 2018 7:01 am
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'}

Re: SendPingAsync Hanging when included in e

Posted: Wed Oct 31, 2018 7:08 am
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.

Re: SendPingAsync Hanging when included in exe

Posted: Wed Oct 31, 2018 8:06 am
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.

Re: SendPingAsync Hanging when included in e

Posted: Wed Oct 31, 2018 10:48 am
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.

Re: SendPingAsync Hanging when included in e

Posted: Wed Oct 31, 2018 10:55 am
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
    }
}

Re: SendPingAsync Hanging when included in e

Posted: Wed Oct 31, 2018 12:44 pm
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.