How to use Ping.SendAsync working with datagridview?(如何使用 Ping.SendAsync 与 datagridview 一起工作?)
问题描述
我有一个应用程序可以 ping datagridview 中的每个 IP,以编译响应 IP RoundtripTime 列表.完成该步骤后,我会将 RoundtripTime 推回 datagridview.
I have an application that pings every IP in the datagridview in order to compile a list of responsive IP RoundtripTime.When finished the step,I will push the RoundtripTime back to datagridview.
...
foreach (DataGridViewRow row in this.gvServersList.Rows)
{
this.current_row = row;
string ip = row.Cells["ipaddr_hide"].Value.ToString();
ping = new Ping();
ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);
ping.SendAsync(ip, 1000);
System.Threading.Thread.Sleep(5);
}
...
private static void ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
var reply = e.Reply;
DataGridViewRow row = this.current_row; //notice here
DataGridViewCell speed_cell = row.Cells["speed"];
speed_cell.Value = reply.RoundtripTime;
}
当我想使用 DataGridViewRow row = this.current_row;
来获取当前行但我得到一个错误 Keyword 'this' is not available in static function.so,如何推送值回datagridview?
When I want to use DataGridViewRow row = this.current_row;
to get the current row but I just get an error Keyword 'this' is not available in static function.so,how to push the value back to datagridview?
谢谢.
推荐答案
KAJ 说的.但是有可能会混淆 ping 请求的结果,因为它们没有连接到网格中的 ip 地址.无法判断哪个主机将首先响应,如果 ping > 5ms 任何事情都可能发生,因为 currentrow 在回调之间发生变化.您需要做的是将 datagridviewrow 引用发送到回调.为此,请使用 SendAsync 的重载:
What KAJ said. But there is a chance of mixing up results of ping requests because they are not connected to ip addresses in grid. One could not tell which host will respond first, and if there is a ping > 5ms anything can happen because currentrow is changing in between callbacks. What you need to do is to send a datagridviewrow reference to a callback. To do that, use an overload of SendAsync:
ping.SendAsync(ip, 1000, row);
在回调中:
DataGridViewRow row = e.UserState as DataGridViewRow;
您可能还想检查 reply.Status 以确保该请求没有超时.
You might also want to check reply.Status to make sure that request did not time-out.
这篇关于如何使用 Ping.SendAsync 与 datagridview 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Ping.SendAsync 与 datagridview 一起工作?
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01