using System.Net.NetworkInformation;
using System;
namespace Gildor.SimpleHostMonitor.Desktop.Utilities
{
///
/// Uses the class
/// to send an ICMP ping to a specified host asynchronously.
///
class Pinger
{
///
/// The maximum timeout in milliseconds of the ping operation.
///
public static readonly int MaxTimeOut = 4000;
///
/// Pings the specified ip.
///
/// The ip.
/// The callback.
public static void Ping(string ip, PingCompletedEventHandler callback)
{
Ping(ip, callback, ip, 3000);
}
///
/// Pings the specified ip.
///
/// The ip.
/// The callback.
/// The user token.
/// The timeout (in milliseconds).
public static void Ping(string ip, PingCompletedEventHandler callback, object userToken, int timeout)
{
if (timeout > MaxTimeOut)
{
throw new ArgumentOutOfRangeException("timeout");
}
//Ping object is disposable. It should be disposed when ping is completed.
Ping ping = new Ping();
ping.PingCompleted += callback;
ping.SendAsync(ip, timeout, userToken);
}
}
}