带有索引的VB.net AddHandler(VB.net AddHandler with index)

编程入门 行业动态 更新时间:2024-10-19 11:42:20
带有索引的VB.net AddHandler(VB.net AddHandler with index)

我有一个多线程程序,可以从互联网上下载不同代理的信息。 我有它工作正常,但我必须为每个线程添加函数,以便我知道正在处理哪个线程。 所以,如果我想要10个线程,我需要10个名为processItems0 processItems1 processItems2的函数,依此类推。 我的所有processItems0函数都将数据传递给另一个带索引的函数。 我希望我可以做一些像processItems(0)这样的事情,这样我就可以拥有1个函数,并且不需要一堆if语句来跟踪数据来自哪个webclient。 如果我想要它,我希望它支持100线程。 什么即时工作,但它不能是最好的方式。 提前致谢

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

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. All my processItems0 function does is pass the data to another function with a index. 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. I want it to support 100 thread if i wanted it to. 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

最满意答案

请记住删除事件处理程序以防止内存泄漏。

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

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

更多推荐

本文发布于:2023-08-05 00:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   VB   net   AddHandler   index

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!