在C#中的Ping上获得更快的结果 [英] Getting Faster result on Ping in C#

查看:421
本文介绍了在C#中的Ping上获得更快的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个工具,以使用.NET中的"PingReply"类测试与某些主机的连接.我的问题是,如果ping结果失败,则需要一段时间才能得到结果.这是局域网环境,所以我已经可以假设如果连接时间超过100毫秒,连接将会失败.如果与主机的连接失败,则下面的代码显示5秒钟后的结果,即5000ms.即使连接失败,我还能得到更快的结果吗?

I'm making a tool to test a connection to certain host using a class "PingReply" in .NET. My problem is it takes a while to get a result if the ping result is a fail. It is LAN environment so i can already assume that the connection is failed if it takes more than 100ms. The code below shows a result after 5 seconds, which is 5000ms, if the connection to the host fails. Can i get the faster result even though the connection is failed?

Ping x = new Ping();
PingReply reply = x.Send(IPAddress.Parse("192.168.0.1")); 
if (reply.Status == IPStatus.Success)
{
    //Do something
}

推荐答案

您可以将超时传递给Ping.Send()方法.请检查超载的成员.

You can pass a timeout to the Ping.Send() method. Please check out the overloaded members.

这篇关于在C#中的Ping上获得更快的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆