Ping.SendAsync to reduce UI freezing

This commit is contained in:
Matthias Mailänder
2013-04-07 19:31:08 +02:00
parent 1069a89332
commit 43492b920d

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading;
namespace OpenRA.Network
{
@@ -75,9 +76,21 @@ namespace OpenRA.Network
{
hasBeenPinged = true;
var pingSender = new Ping();
PingReply reply = pingSender.Send(Address.Split(':')[0]);
if (reply != null && reply.Status == IPStatus.Success)
Latency = (int)reply.RoundtripTime;
pingSender.PingCompleted += new PingCompletedEventHandler(pongRecieved);
AutoResetEvent waiter = new AutoResetEvent(false);
pingSender.SendAsync(Address.Split(':')[0], waiter);
}
}
void pongRecieved(object sender, PingCompletedEventArgs e)
{
if (e.Cancelled || e.Error != null)
Latency = -1;
else
{
PingReply pong = e.Reply;
if (pong != null && pong.Status == IPStatus.Success)
Latency = (int)pong.RoundtripTime;
else
Latency = -1;
}