[英]VB.net AddHandler with index
I have a multithread program that download info from the internet off different proxies. 我有一个多线程程序,可以从互联网上通过不同的代理下载信息。 I have it working fine but I have to add functions for each thread so that I know which thread is being processed.
我的工作正常,但我必须为每个线程添加功能,以便知道正在处理哪个线程。 so if I want 10 thread I need 10 functions named processItems0 processItems1 processItems2 and so on.
因此,如果我要10个线程,则需要10个名为processItems0 processItems1 processItems2的函数,依此类推。 All my processItems0 function does is pass the data to another function with a index.
我所有的processItems0函数所做的就是将数据传递给带有索引的另一个函数。 I wish I could do something thing like processItems(0) so that I can have 1 function and didn't need a stack of if statements to track which webclient the data is coming from.
我希望我可以做类似processItems(0)这样的事情,这样我就可以拥有1个函数,并且不需要一堆if语句来跟踪数据来自哪个Web客户端。 I want it to support 100 thread if i wanted it to.
我希望它支持100个线程。 what im doing works but it cant be the best way.
我正在做什么,但这不是最好的方法。 Thanks in advance
提前致谢
Dim wc As New WebClient
''' if statements that i want to get rid of
If wcn = 0 Then
AddHandler wc.UploadStringCompleted, AddressOf processItems0
ElseIf wcn = 1 Then
AddHandler wc.UploadStringCompleted, AddressOf processItems1
end if
wc.Proxy = wp(wcn)
Dim u As New Uri(laurl)
wc.UploadStringAsync(u, data)
''' individual functions for each webclient i want to run.. t
Private Sub processItems0(ByVal sender As Object, ByVal e As UploadStringCompletedEventArgs)
If e.Cancelled = False AndAlso e.Error Is Nothing Then
processData(CStr(e.Result), 0)
End If
End Sub
Private Sub processItems1(ByVal sender As Object, ByVal e As UploadStringCompletedEventArgs)
If e.Cancelled = False AndAlso e.Error Is Nothing Then
processData(CStr(e.Result), 1)
End If
End Sub
Private Sub processData(data As String, wcn As Integer)
'process data
end Sub
Please remember to remove your event handlers to prevent memory leaks. 请记住要删除事件处理程序,以防止内存泄漏。
Public Class ProxyWrapper
Inherits WebClient
Private _index As Integer
Public Sub New(ByVal index As Integer)
_index = index
End Sub
Public ReadOnly Property Index As Integer
Get
Return _index
End Get
End Property
Public Sub RegisterEvent()
AddHandler Me.UploadStringCompleted, AddressOf processItems
End Sub
Public Sub UnregisterEvent()
RemoveHandler Me.UploadStringCompleted, AddressOf processItems
End Sub
Private Sub ProcessItems(ByVal sender As Object, ByVal e As UploadStringCompletedEventArgs)
If e.Cancelled = False AndAlso e.Error Is Nothing Then
ProcessData(CStr(e.Result), _index)
End If
End Sub
Private Sub ProcessData(ByVal res As String, ByVal index As Integer)
' Handle data
End Sub
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.