我正在使用Ping类定期ping几台主机。 如果下一次ping的时间到了,但是之前的ping尚未完成,我会调用SendAsyncCancel来终止它。
问题出现在如果我禁用网络接口,这种情况下异步回调将永远不会被调用,并且对SendAsyncCancel的调用也永远不会返回。
更多信息如下:我正在使用.NET 3.5和C# VS2008 express在Windows 7 x64上进行开发。 我从窗体的Timer.Tick回调中调用ping。 我仅为每个主机创建一次Ping类(总共3个主机,但只有一个主机时相同)。 超时设置为5秒。 该问题可以100%重现。
我发现的所有问题都与ping崩溃在多次创建/销毁ping类时有关,但这并不是我的情况。
问题出现在如果我禁用网络接口,这种情况下异步回调将永远不会被调用,并且对SendAsyncCancel的调用也永远不会返回。
更多信息如下:我正在使用.NET 3.5和C# VS2008 express在Windows 7 x64上进行开发。 我从窗体的Timer.Tick回调中调用ping。 我仅为每个主机创建一次Ping类(总共3个主机,但只有一个主机时相同)。 超时设置为5秒。 该问题可以100%重现。
我发现的所有问题都与ping崩溃在多次创建/销毁ping类时有关,但这并不是我的情况。
using System;
using System.Net.NetworkInformation;
using System.Windows.Forms;
namespace TestPing {
public partial class Form1 : Form {
private Ping Pinger;
public Form1()
{
InitializeComponent();
Pinger = new Ping();
Pinger.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
}
private void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
txtResult.Text = e.Cancelled ? "Cancelled" : e.Reply.Status.ToString();
}
private void butSend_Click(object sender, EventArgs e)
{
txtResult.Text = "(result)";
txtStatus.Text = "SendAsync() calling...";
Pinger.SendAsync(txtHost.Text, null);
txtStatus.Text = "SendAsync() done.";
}
private void butCancel_Click(object sender, EventArgs e)
{
txtStatus.Text = "SendAsyncCancel() calling...";
Pinger.SendAsyncCancel();
txtStatus.Text = "SendAsyncCancel() done.";
}
}
}