我用的是窗口程序写的PING程序,使用了里面的SendAsync()异步方法。请高手指教一下,怎么会停止呢?
private void Form1_Load(object sender, EventArgs e)
{
PingAddress();//执行这个方法的时候,是停止在arEvent.WaitOne();之后,造成窗体程序弹不出来。
}
public void PingAddress()
{
//Thread.Sleep(1000);
AutoResetEvent arEvent = new AutoResetEvent(false);
Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
p.SendAsync("172.18.0.25", arEvent);
arEvent.WaitOne();
}
void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("Cancelled");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
//((AutoResetEvent)e.UserState).Set();
} if (e.Error != null)
{
MessageBox.Show("Error");
// Let the main thread resume.
//((AutoResetEvent)e.UserState).Set();
} //PingReply reply = e.Reply; //PingState(reply);
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
private void Form1_Load(object sender, EventArgs e)
{
PingAddress();//执行这个方法的时候,是停止在arEvent.WaitOne();之后,造成窗体程序弹不出来。
}
public void PingAddress()
{
//Thread.Sleep(1000);
AutoResetEvent arEvent = new AutoResetEvent(false);
Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
p.SendAsync("172.18.0.25", arEvent);
arEvent.WaitOne();
}
void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("Cancelled");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
//((AutoResetEvent)e.UserState).Set();
} if (e.Error != null)
{
MessageBox.Show("Error");
// Let the main thread resume.
//((AutoResetEvent)e.UserState).Set();
} //PingReply reply = e.Reply; //PingState(reply);
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货