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 一起工作?
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01