0

我正在使用Ping来自的类System.Net.NetworkInformation,我需要将一些字符串数据发送到PingCompleted事件处理程序的事件函数。我试过这个:

void ex_ping_PingCompleted(object sender, PingCompletedEventArgs e, string adress)
{

}

...

ex_ping.PingCompleted += (sender1, args) => ex_ping_PingCompleted(sender1, new 
PingCompletedEventArgs(), adress);

但它告诉我PingCompletedEventArgs没有构造函数。我试图制作自己的事件参数:

public class ProgressEventArgs : PingCompletedEventArgs
{
            public string adress;
            public ProgressEventArgs(string ex_adress)
            {
                adress = ex_adress;
            }
}

这里同样的错误。我只想知道我 ping 了哪个地址,e.Reply.Address在事件函数中给了我 IP,我需要站点名称。

编辑:我自己想出来的:

 ex_ping.PingCompleted += (sender1, args) => ex_ping_PingCompleted(sender1, args,adress); 

    void ex_ping_PingCompleted(object sender, PingCompletedEventArgs e,string ex_adress)        {
    }

工作正常,/关闭

4

2 回答 2

1

订阅活动

 ex_ping.PingCompleted += ex_ping_PingCompleted;

并在处理程序中获取地址

void ex_ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
   // verify if operation was not canceled or some error occured

   var address = e.Reply.Address;
   IPHostEntry entry = Dns.GetHostEntry(address);
   var siteName = entry.HostName;
}
于 2013-01-29T23:33:13.167 回答
0

你在UserState财产之后:

void Ping()
{
    string address = "google.com";
    ex_ping.PingCompleted+=ex_ping_PingCompleted;
    ex_ping.SendAsync(address, 500, buffer, options, address);
}

void ex_ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
    // Will give you the hostname
    var address = e.UserState;
}
于 2015-04-13T06:53:33.907 回答