如何在不冻结的情况下对 IP 执行 ping 系统 VB?
How to make a pinging system to IP without freeze VB?
我正在尝试制作一个 ping 系统来 ping 我的 VB windows 表单中的 IP。当我在文本框中输入文本时,我使用的当前 ping 系统往往会冻结应用程序。我相信有更好的方法可以做到这一点。
有什么地方出错了吗?
定时器:
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
If Timer4.Enabled = True Then
Dim pingreq As Ping = New Ping()
Dim rep As PingReply = pingreq.Send(NsTextBox1.Text)
NsTextBox4.Text = NsTextBox4.Text + Environment.NewLine + ("Reply from " + NsTextBox1.Text + ":" + NsTextBox2.Text + "| time= " + rep.RoundtripTime.ToString() + "ms")
If rep.RoundtripTime = 0 Then
Timer4.Enabled = False
End If
End If
End Sub
按钮:
Private Sub NsButton2_Click(sender As Object, e As EventArgs) Handles NsButton2.Click
Timer4.Enabled = True
End Sub
您可以摆脱计时器,只等待一个任务,该任务会直接从按钮处理程序执行 ping,如下所示:
Private Async Sub NsButton2_Click(sender As Object, e As EventArgs) Handles NsButton2.Click
Dim t = Task(Of PingReply).Run(
Function()
Dim pingreq As Ping = New Ping()
Dim rep As PingReply = pingreq.Send(NsTextBox1.Text)
Return rep
End Function)
Dim result = Await t
NsTextBox4.Text = NsTextBox4.Text + Environment.NewLine +
("Reply from " + NsTextBox1.Text + ":" + NsTextBox2.Text +
"| time= " + result.RoundtripTime.ToString() + "ms")
End Sub
我正在尝试制作一个 ping 系统来 ping 我的 VB windows 表单中的 IP。当我在文本框中输入文本时,我使用的当前 ping 系统往往会冻结应用程序。我相信有更好的方法可以做到这一点。
有什么地方出错了吗?
定时器:
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
If Timer4.Enabled = True Then
Dim pingreq As Ping = New Ping()
Dim rep As PingReply = pingreq.Send(NsTextBox1.Text)
NsTextBox4.Text = NsTextBox4.Text + Environment.NewLine + ("Reply from " + NsTextBox1.Text + ":" + NsTextBox2.Text + "| time= " + rep.RoundtripTime.ToString() + "ms")
If rep.RoundtripTime = 0 Then
Timer4.Enabled = False
End If
End If
End Sub
按钮:
Private Sub NsButton2_Click(sender As Object, e As EventArgs) Handles NsButton2.Click
Timer4.Enabled = True
End Sub
您可以摆脱计时器,只等待一个任务,该任务会直接从按钮处理程序执行 ping,如下所示:
Private Async Sub NsButton2_Click(sender As Object, e As EventArgs) Handles NsButton2.Click
Dim t = Task(Of PingReply).Run(
Function()
Dim pingreq As Ping = New Ping()
Dim rep As PingReply = pingreq.Send(NsTextBox1.Text)
Return rep
End Function)
Dim result = Await t
NsTextBox4.Text = NsTextBox4.Text + Environment.NewLine +
("Reply from " + NsTextBox1.Text + ":" + NsTextBox2.Text +
"| time= " + result.RoundtripTime.ToString() + "ms")
End Sub