PHP:
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.Net;
using System.Net.NetworkInformation;
using System.Threading;
namespace SimplePinger
{
public partial class Form1 : Form
{
#region Variables
int PingAmount = 5;
Ping pingSender = new Ping();
PingOptions options = new PingOptions(64, true);
byte[] buffer = new byte[32];
#endregion
public Form1()
{
InitializeComponent();
updatePingValue(5); //Set defualt to 5
// no longer using async = no callback needed
//pingSender.PingCompleted += new PingCompletedEventHandler(PingCallback);
// wtfbbq??
/*Ping Options
Action CallbackAction = new Action(() => pingSender.PingCompleted += new PingCompletedEventHandler(PingCallback));
CallbackAction.Invoke();*/
}
private void PingTimes_Scroll(object sender, EventArgs e)
{
updatePingValue(PingTimes.Value);
}
private void updatePingValue(int Value)
{
PingAmount = PingTimes.Value;
PingAmountLabel.Text = "Pings to send: " + PingAmount;
}
private void startPing_Click(object sender, EventArgs e)
{
outputBox.Clear();
/*
* Start the thread to run the InitiateThreadPinger()
*/
//new Thread(InitiateThreadPinger).Start();
// for fire and forget threads the threadpool is better
ThreadPool.QueueUserWorkItem(InitiateThreadPinger, addressBox.Text);
}
public void InitiateThreadPinger(object state)
{
/*
* Loop 1-10 and run a ping on the host
*/
options.DontFragment = true;
for (int i = 0; i <= PingAmount; i++)
{
// no longer using async = no callback needed
this.AddCallbackToForm(pingSender.Send((string)state, 120, buffer, options));
}
}
/*public void PingCallback(object sender, PingCompletedEventArgs e)
{
if (e.Cancelled)
{
EnableFormElements();
}
if (e.Error != null)
{
MessageBox.Show("Ping Failed:" + e.Error.ToString());
return;
}
PingReply reply = e.Reply;
AddCallbackToForm(reply);
}*/
public void AddCallbackToForm(PingReply Reply)
{
// gets called from the other thread => invoke
this.Invoke((Action)(() =>
{
if (Reply == null)
{
return;
}
if (Reply.Status == IPStatus.Success)
{
outputBox.Text += "Success: " + Reply.Address + " - " + Reply.RoundtripTime + "ms";
outputBox.Text += Environment.NewLine;
}
else
{
outputBox.Text += "Failed...";
outputBox.Text += Environment.NewLine;
}
}));
}
public void EnableFormElements()
{
startPing.Enabled = true;
PingTimes.Enabled = true;
}
public void DisableFormElements()
{
startPing.Enabled = false;
PingTimes.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
^Works here. See comments.