因为你使用lock,这个导致了一个timeout(假设其中某个timeout了),那么其余的势必会timeout(因为一个延时,其余的多线程无法访问这个事件,因为你加了lock)。
解决方案,移除lock:
namespace Csharp
{
public class Test
{
static AutoResetEvent flag = new AutoResetEvent(false);
static void Main(string[] args)
{
Ping p = new Ping();
p.PingCompleted += p_PingCompleted;
p.SendAsync("www.baidu.com", 1000, null);
flag.WaitOne();
//延时的
p = new Ping();
p.PingCompleted += p_PingCompleted;
p.SendAsync("www.google.com", 1000, null);
flag.WaitOne();
p = new Ping();
p.PingCompleted += p_PingCompleted;
//正常的
p.SendAsync("www.online.sh.cn", 1000, null);
flag.WaitOne();
}
static void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
if (e.Reply != null)
{
Console.WriteLine(e.Reply.Address + "\t状态:" + e.Reply.Status);
flag.Set();
}
}
}
}
If you think one reply solves your problem, please mark it as
An Answer, if you think someone's reply helps you, please mark it as a
Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at:
Spam Report