The code below is what I'm using to ping 8.8.8.8, but some times when I press start it is just packet loss which this counts, that is strange because I'm sure I don't have that much of packet loss, then I pause it and start again and it works just fine, it doesn't make sense to me, I'm at a loss.
I added Thread.Sleep(); because I guessed that the program is not probably loaded when I start it, but that's strange too ! it is such a small and simple program it shouldn't have such problems
I also have a question what time out time should I put for this , right now the app sends a packet each sec , and drops it every 900ms , should I put it higher than 900 or would that drop the performance?
public partial class Form1 : Form
{
public int PingTime;
public int pingor = 0;
public int pingur = 0;
public int maxping;
public int minping;
public Int64 avgping;
public string avrage;
public int ping_no;
public string fail;
public string msg1 = " Packet Lost ";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true )
{
timer1.Enabled = false;
Thread.Sleep(1000);
}
else if (timer1.Enabled == false)
{
timer1.Enabled = true;
Thread.Sleep(1000);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
bool pingable = false;
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "1";
byte[] buffer = Encoding.ASCII.GetBytes(data);
Ping pinger = new Ping();
try
{
//37.58.117.146
//8.8.8.8
PingReply reply = pinger.Send("8.8.8.8", 900, buffer, options);
pingable = reply.Status == IPStatus.Success;
string pingtime = reply.RoundtripTime.ToString();
int newpingtime = Convert.ToInt32(pingtime);
if (reply.Status == IPStatus.Success)
{
label1.Text = reply.RoundtripTime.ToString();
}
else
{
label1.Text = msg1;
}
//_________________________Max Ping
if (maxping == 0)
{
maxping = Convert.ToInt32(newpingtime);
}
else if (newpingtime >= Convert.ToInt32(maxping))
{
maxping = Convert.ToInt32(newpingtime);
}
lblmax.Text = maxping.ToString();
//_________________________Min Ping
if (reply.Status == IPStatus.Success && minping == 0 && newpingtime == 0)
{
lblmin.Text = minping.ToString();
}
else if (reply.Status == IPStatus.Success && lblmin.Text == " - - -" && newpingtime >= 0)
{
minping = newpingtime;
lblmin.Text = minping.ToString();
}
else if (reply.Status == IPStatus.Success && newpingtime < minping)
{
minping = newpingtime;
lblmin.Text = minping.ToString();
}
//_________________________Ping AVG
if (reply.Status == IPStatus.Success)
{
avgping = avgping + newpingtime;
ping_no = ping_no + 1;
lblavg.Text = Convert.ToString(avgping / ping_no);
}
}
catch (PingException error)
{
MessageBox.Show(error.Message);
}
if (pingable == true)
{
pingor++;
}
if (pingable == false)
{
pingur++;
}
lblrecived.Text = Convert.ToString(pingor);
lbllost.Text = Convert.ToString(pingur);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Thread.Sleep(900);
}
Thanks in advance, Just letting you know that I'm not having packet loss more than 2% and this shouldn't show 40 packet loss and 0 received ! also it is working just fine 80% of the times!
Once again It happens when I pause and start it again fast or some times the first time I start it.