Evo ova funkcija radi dobro. Nadjeno i malo editirano iz OReailly knjige
Code:
public void Ping_Function(string hostname)
{
System.Net.NetworkInformation.Ping pinger = new System.Net.NetworkInformation.Ping();
PingReply reply = pinger.Send(hostname);
DisplayPingReplyInfo(reply);
pinger.PingCompleted += new PingCompletedEventHandler(pinger_PingCompleted);
pinger.SendAsync(hostname, "oreilly ping");
}
private void DisplayPingReplyInfo(PingReply reply)
{
if (reply.Status == IPStatus.Success)
{
string result = "Results from pinging " + reply.Address
+ "\tFragmentation allowed?: " + !reply.Options.DontFragment
+ "\tTime to live: " + reply.Options.Ttl
+ "\tRoundtrip took: " + reply.RoundtripTime
+ "\tStatus: " + reply.Status.ToString();
this.RichTextBoxPing.Text = result;
}
else
{
string result = reply.Status.ToString ();
this.RichTextBoxPing.Text = result;
}
}
private void pinger_PingCompleted(object sender, PingCompletedEventArgs e)
{
PingReply reply = e.Reply;
DisplayPingReplyInfo(reply);
string result;
if(e.Cancelled)
{
result = "Ping for " + e.UserState.ToString() + " was cancelled";
this.RichTextBoxPing.Text = result;
}
else if (e.Error != null)
{
result = "Exception thrown during ping: " + e.Error.ToString();
this.RichTextBoxPing.Text = result;
}
}
xxxrugby: "We are all philosophers, when question is about politics!"