using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.NetworkInformation;
namespace PingStatus
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Counts the pings
private int pingsSent;
// Can be used to notify when the operation completes
AutoResetEvent resetEvent = new AutoResetEvent(false);
private void button1_Click(object sender, EventArgs e)
{
SendPing();
}
private void SendPing()
{
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
// Create an event handler for ping complete
pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_Complete);
// Create a buffer of 32 bytes of data to be transmitted.
byte[] packetData = Encoding.ASCII.GetBytes("................................");
// Jump though 50 routing nodes tops, and don't fragment the packet
PingOptions packetOptions = new PingOptions(50, true);
// Send the ping asynchronously
pingSender.SendAsync(txtIP.Text, 5000, packetData, packetOptions, resetEvent);
}
private void pingSender_Complete(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
MessageBox.Show("Ping was canceled...rn");
// The main thread can resume
((AutoResetEvent)e.UserState).Set();
}
else if (e.Error != null)
{
MessageBox.Show(String.Format("An error occured: {0}{1}", e.Error, Environment.NewLine));
// The main thread can resume
((AutoResetEvent)e.UserState).Set();
}
else
{
PingReply pingResponse = e.Reply;
// Call the method that displays the ping results, and pass the information with it
//ShowPingResults(pingResponse);
if (pingResponse == null)
{
// We got no response
MessageBox.Show("Нет ответа.rnrn");
return;
}
else if (pingResponse.Status == IPStatus.Success)
{
// We got a response, let's see the statistics
MessageBox.Show(String.Format("Компьютор доступен. IP адресс {0}", pingResponse.Address));
return;
}
else
{
// The packet didn't get back as expected, explain why
MessageBox.Show(String.Format("Неудачный запрос: {0}", pingResponse.Status));
return;
}
}
}
}
}
0.00 (0%) 0 votes








