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.
Locked
User avatar
Genx13
Posts: 12
Joined: Fri May 25, 2018 7:46 am

SendPingAsync Hanging when included in e

Post by Genx13 » Wed Oct 31, 2018 7:01 am

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: 7121
Joined: Tue May 29, 2007 4:43 pm

Re: SendPingAsync Hanging when included in e

Post by Alexander Riedel » Wed Oct 31, 2018 7:08 am

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
Joined: Fri May 25, 2018 7:46 am

Re: SendPingAsync Hanging when included in exe

Post by Genx13 » Wed Oct 31, 2018 8:06 am

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SendPingAsync Hanging when included in e

Post by jvierra » Wed Oct 31, 2018 10:48 am

"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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SendPingAsync Hanging when included in e

Post by jvierra » Wed Oct 31, 2018 10:55 am

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SendPingAsync Hanging when included in e

Post by jvierra » Wed Oct 31, 2018 12:44 pm

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.

Locked