public Task <bool> IsReachable(string host, TimeSpan timeout) { return(Task <bool> .Run(() => { if (!DeviceNetworkInformation.IsNetworkAvailable) { return false; } AutoResetEvent e = new AutoResetEvent(false); bool isReachable = false; NameResolutionCallback d = delegate(NameResolutionResult result) { isReachable = result.NetworkErrorCode == NetworkError.Success; e.Set(); }; DeviceNetworkInformation.ResolveHostNameAsync(new System.Net.DnsEndPoint(host, 0), d, this); e.WaitOne(timeout); return isReachable; })); }
/// <summary> /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed. /// <example> /// nameresolutioncallback.BeginInvoke(result, callback); /// </example> /// </summary> public static IAsyncResult BeginInvoke(this NameResolutionCallback nameresolutioncallback, NameResolutionResult result, AsyncCallback callback) { if (nameresolutioncallback == null) { throw new ArgumentNullException("nameresolutioncallback"); } return(nameresolutioncallback.BeginInvoke(result, callback, null)); }