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 一起工作?
基础教程推荐
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
