async void Test()
{
await Ping("www.google.com");
}
Task Ping(string host)
{
var tcs = new TaskCompletionSource<object>();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "ping.exe";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Arguments = host;
var proc = Process.Start(psi);
proc.OutputDataReceived += (s, e) => {
Action action = () => textBox1.Text += e.Data + Environment.NewLine;
this.Invoke(action);
};
proc.Exited += (s, e) => tcs.SetResult(null);
proc.EnableRaisingEvents = true;
proc.BeginOutputReadLine();
return tcs.Task;
}
RESULT
Pinging www.google.com [64.15.117.154] with 32 bytes of data:
Reply from 64.15.117.154: bytes=32 time=50ms TTL=55
Reply from 64.15.117.154: bytes=32 time=45ms TTL=55
Reply from 64.15.117.154: bytes=32 time=45ms TTL=55
Reply from 64.15.117.154: bytes=32 time=46ms TTL=55
Ping statistics for 64.15.117.154:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 45ms, Maximum = 50ms, Average = 46ms
Having said that, I would use the Ping
class instead of launching an external application
var ping = new System.Net.NetworkInformation.Ping();
var pingReply = await ping.SendPingAsync("www.google.com");
Console.WriteLine("{0} {1} {2}",pingReply.Address,
pingReply.RoundtripTime,
pingReply.Status);