我编写了一个应用程序,它基本上会对主机列表进行持续ping操作并记录结果。
我有一个功能...
Public Function doping(ByRef host As String)
Dim Result As Net.NetworkInformation.PingReply
Dim SendPing As New Net.NetworkInformation.Ping
Dim ResponseTime As Long
Dim timestamp As String = System.DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")
Try
Result = SendPing.Send(host, 300)
ResponseTime = Result.RoundtripTime
If Result.Status = Net.NetworkInformation.IPStatus.Success Then
DynaLogging(timestamp & " - Reply from " & host & ": bytes=32 time=" & ResponseTime.ToString & " TTL=??", host & ".log")
TextBox2.Text = timestamp & " - Reply from " & host & " : time =" & ResponseTime.ToString & br & TextBox2.Text
Else
DynaLogging(timestamp & " - No reply received from " & host, host & ".log")
TextBox2.Text = timestamp & " - No reply received from " & host & br & TextBox2.Text
End If
Catch ex As Exception
End Try
End Function
我有一个计时器,目前在后台工作人员中触发了以下内容......
On Error Resume Next
For Each hostyhost As String In ListBox1.Items
doping(hostyhost)
Next
这是一种方法,直到列表中的一个主机没有响应,然后后台工作人员等待,因为它一次只能处理一个ping,因此如果一个主机被延迟,其余主机必须等待它们之前检查,整个过程放缓。
我可以通过某种方式在运行时动态创建多个后台工作程序,但我不知道如何动态创建后台工作程序,可以传递参数。
我看了this post关于在运行时创建bgw的问题,但不知道如何根据我的需要部署它。
有人能指出我正确的方向吗?
答案 0 :(得分:1)
为什么不是Parallel ForEach?
Dim Items As List(Of String) = New List(Of String)
For Each item As String In ListBox1.Items
Items.Add(item.ToString())
Next
Parallel.ForEach(Items, Sub(hostyhost)
doping(hostyhost)
End Sub)
它包含在System.Threading命名空间下的.NET Framework 4.0中,几乎可以处理所有内容,而无需编写一百万行代码。